Search code examples
filecordova

download files in phonegap application


I found a function which downloads files to my device sd card

application.downloadFile = function(file, callback)
{
    window.requestFileSystem(
    LocalFileSystem.PERSISTENT, 0, 
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
        "dummy.html", {create: true, exclusive: false}, 
        function gotFileEntry(fileEntry) {
        var sPath = fileEntry.fullPath.replace("dummy.html","");
        var fileTransfer = new FileTransfer();
        fileEntry.remove();

        fileTransfer.download(
            application.api+file,
            sPath + "assas.json",
            function(theFile) {
                console.log("download complete: " + theFile.toURI());
                application.dataPath = theFile.toURI();
                callback();
            },
            function(error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code: " + error.code);
            }
          );
        });
    });
};

The code works fine but it download the code in my sd card and I'd like to download my file directly in my application.

In www/datas

but I don't know really how to do this.


Solution

  • Unfortunately the WWW folder is read only and you cannot write to it. You can only write to the file system.