How to download file to Download's directory w

2019-07-12 21:28发布

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.

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-12 22:02

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

查看更多
唯我独甜
3楼-- · 2019-07-12 22:13

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));
  });
查看更多
登录 后发表回答