Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file to Downloads folder of device using Cordova FileTransfer

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?

like image 742
Shoreki Avatar asked Feb 05 '16 09:02

Shoreki


Video Answer


1 Answers

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.

like image 154
Let'sRefactor Avatar answered Sep 25 '22 06:09

Let'sRefactor