How to download file to Download's directory w

2019-07-12 21:32发布

问题:

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));
  });