How to download file to Download's directory with Ionic Framework?

牧云@^-^@ 提交于 2020-01-04 13:36:57

问题


I'm trying with ngCordova but the cordova.file.documentsDirectory property is null.

I have also tried combining the use of ngCordova with requestFileSystem, but still, the file is not saved in the "Download's".

Example of my code:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(directory) {
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        encodeURI("http://example.com/sample.pdf"),
        directory.root.nativeURL + 'sample.pdf',
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false
    );
}, function(err){console.error(err)});

Thanks.


回答1:


cordova.file.externalRootDirectory + '/Download/' + 'sample.pdf' instead of directory.root.nativeURL + 'sample.pdf'




回答2:


I solved it via the following code:

const ROOT_DIRECTORY = 'file:///sdcard//';
const downloadFolderName = 'Download';


this.file.createDir(ROOT_DIRECTORY, downloadFolderName, true)
  .then((entries) => {
 //then your code
 fileTransfer.download(fileLocation,this.ROOT_DIRECTORY+this.downloadFolderName+'/'+ 'sample.pdf').then((entry) => {
}
//ends of your code ^^
})
  .catch((error) => {
    alert('error' + JSON.stringify(error));
  });


来源:https://stackoverflow.com/questions/41491042/how-to-download-file-to-downloads-directory-with-ionic-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!