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

10,332

Solution 1

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

Solution 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));
  });
Share:
10,332
Jorge Olaf
Author by

Jorge Olaf

:D

Updated on June 26, 2022

Comments

  • Jorge Olaf
    Jorge Olaf almost 2 years

    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.

  • Jorge Olaf
    Jorge Olaf over 7 years
    This Question is about download file to "Downloads folder" of Android, not about "cordova.file.documentsDirectory null". Definitely, you answer is too poor and not is constructive.
  • Wade Tyler
    Wade Tyler over 7 years
    You said "I'm trying with ngCordova but the cordova.file.documentsDirectory property is null"
  • Jorge Olaf
    Jorge Olaf over 7 years
    Yes, and I says "also tray with "also tried combining the use of ngCordova with requestFileSystem". This question not is about documentsDirectory, is about How I can download file and store it on "Downloads Folder" on Android.
  • Jorge Olaf
    Jorge Olaf over 7 years
    This look much better. I'll try it. Thanks! +1
  • Wade Tyler
    Wade Tyler over 7 years
    @OlafErlandsen No problem. Check cordova.apache.org/docs/en/latest/reference/…
  • Jorge Olaf
    Jorge Olaf over 7 years
    This answer works perfectly. I clarify that it is necessary to add write permissions for the application, otherwise it will not be able to download. Thanks @WadeTyler
  • Wade Tyler
    Wade Tyler over 7 years
    @OlafErlandsen No problem. Glad to help.
  • Luckylooke
    Luckylooke about 7 years
    @OlafErlandsen Can you tell me what permission I need for Download directory? I am getting /storage/emulated/0/Download/montages.json: open failed: EACCES (Permission denied). I have tried READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE and MANAGE_DOCUMENTS permissions. Did not helped :/
  • Jorge Olaf
    Jorge Olaf about 7 years
    Well, whats Android version are using you?
  • noor
    noor over 6 years
    @Luckylooke Have you found a solution for this ? I am facing the same issu e
  • Luckylooke
    Luckylooke over 6 years
    @noor yes I have succeeded with combination of manifest permissions and cordova-plugin-android-permissions for runtime permissions needed for android 5+