7

I am using Cordova FileTransfer object to download a file from a url to device.

var fileTransfer = new FileTransfer();
var path = cordova.file.dataDirectory;
 fileTransfer.download(
        fileUrl,
        path + "/sample.pdf",
        function(theFile) {
            console.log("download complete: " + theFile.toURI());
            alert("File downloaded to "+cordova.file.dataDirectory);
        },
        function(error) {              
            console.log(JSON.stringify(error));
        }
    );

In this case the file is downloaded to data/data/com.fileDemo/files/ (I am not sure whether download is success as I can't access this folder. Getting success message as download complete: file:///data/data/com.fileDemo/files/sample.pdf). How can I use the same method to download a file to "Downloads" folder of the android device?

Shoreki
  • 1,057
  • 4
  • 14
  • 33

1 Answers1

5

In Cordova, with FileTransfer, you can request TEMPORARY or PERSISTENT file system

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fail);
  • iOS
    • PERSISTENT will return the Documents directory,
    • TEMPORARY will return the Caches directory
  • Android
    • PERSISTENT will returns the root of the SD card/phone memory
    • TEMPORARY will return a folder inside the data folder.

Refer File API & FileTransfer for more info.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
  • 4
    I just tried this, but the gotFs points to the application's data folder. not the Download folder right under the root. Is that possible to access the Download folder that under the root directory? – Expert wanna be Aug 02 '16 at 07:07
  • 3
    For those who still stuck this issue, either `cordova.file.externalRootDirectory + 'Download/'` or `fileSystem.root.toURL() + 'Download/'`should work – REI Aug 27 '17 at 07:45