Olaf Erlandsen
Olaf Erlandsen

Reputation: 6036

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

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.

Upvotes: 3

Views: 11544

Answers (2)

Omer Kamil
Omer Kamil

Reputation: 81

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

Upvotes: 2

Wade Tyler
Wade Tyler

Reputation: 246

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

Upvotes: 8

Related Questions