How do you download a file to Android's Downloads folder using Phonegap?
Asked Answered
R

4

7

I have successfully downloaded a file to my Android phone using Phonegap's File API. I would like to download the file to the Downloads folder on my phone. For example, if you download an attachment from an email, the attachment goes to your Downloads folder. Here is my JS code, which downloads the file to "file://mnt/sdcard/":

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
  fileSystem.root.getFile('myfile.jpg', {
    create: true, 
    exclusive: false
  }, function(fileEntry) {
    var localPath = fileEntry.fullPath,
        fileTransfer = new FileTransfer();        
    fileTransfer.download(uri, localPath, function(entry) {
      console.log("download complete: " + entry.fullPath);
    }, function (error) {
    console.log('download error: ' + error.code);
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
  });
  }, downloadError);
}, downloadError);

There has to be a way to access the Downloads folder, because I see this functionality all of the time in other apps.

Reproachless answered 24/9, 2012 at 22:54 Comment(0)
R
3

You can send the file to the download folder by specifying it in the getFile method...

getfile('download/myfile.jpg' ...)

This doesn't trigger the DownloadManager which notifies you when a file has been downloaded. I am still trying to find a solution for having access to the DownloadManager class through phonegap. I have asked this question here How do you download a file to Android's Downloads folder using Phonegap?

Reproachless answered 28/9, 2012 at 0:57 Comment(1)
Is it possible to save the downloaded file into a custom directory?Kacikacie
A
2

I have created a plugin which downloads file using download manager and shows progress bar along the way

https://github.com/vasani-arpit/cordova-plugin-downloadmanager

//after device is ready
var fail = function (message) {    
   alert(message)
}
var success = function (data) {
   console.log("succes");
}
cordova.plugins.DownloadManager.download("Your URL to download", success, fail);

I hope it helps.

Agitation answered 29/6, 2017 at 15:27 Comment(0)
M
1

I had the same problem, but I solved it like this:

//if IOS cordova.file.documentsDirectory
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (fileEntry) {
  var filepath = fileEntry.toURL() + filename;
  var fileTransfer = new FileTransfer();
  console.log('FilePath ' + filepath);
  fileTransfer.download(uri, filepath,
    function (fileEntry) {
      console.log("download complete: " + fileEntry.toURL());
    },
    function (error) {
      console.log("ErrorDownload: " + JSON.stringify(error));
    },
    true,
    {}
  );
});
Mesoderm answered 30/6, 2016 at 4:52 Comment(1)
Which plugin did you use ?Egg
D
-1

Use this plugin: https://github.com/sgrebnov/cordova-plugin-background-download. I use it in my Cordova app and it works fine.

Decastro answered 11/2, 2015 at 23:53 Comment(1)
did not work for me, does not look like it is well supported.Farrell

© 2022 - 2024 — McMap. All rights reserved.