Hitesh Tailor
Hitesh Tailor

Reputation: 3

Phonegap & iOS simulator - cannot save downloaded file

I'm using Phonegap/cordova and writing a Android/iOS app which will download json data from my server and store locally on device for offline usage. On android, this works perfectly. I don't have an iOS device therefore relying on iOS simulator, and it throws me a "could not create target file" type error.

downloadFile:function(path,uri){
  var fileTransfer = new FileTransfer();

  fileTransfer.download(
    encodeURI(path),
    app.getStorageLocation()+"files/"+uri,
    function(entry) {
      console.log("download complete: " + entry.toURL());
      app.progressMove();
    },
    function(error) {
      console.log("download error source " + error.source);
      console.log("download error target " + error.target);
      console.log("upload error code" + error.code);
    },
    false);

}

The getStorageLocation function is:

getStorageLocation:function(){
    if(device.platform == 'Android'){
        return cordova.file.externalApplicationStorageDirectory;
    }
    else if(device.platform == 'iOS'){
        return cordova.file.documentsDirectory;
    }else{
        throw new Error('Unsupported platform: '+device.platform);
    }
}

On iOS simulator, it does return the Documents directory, but the above fails to write to it. Would this just be a Simulator bug or have I done something wrong?

thanks!

Upvotes: 0

Views: 1403

Answers (2)

Hitesh Tailor
Hitesh Tailor

Reputation: 3

excellent! got it working. Additionally found that not all files were problematic. Apple doesn't like custom file types nor file names with "%20" in them. Fixing all the above has worked!

Upvotes: 0

Uttam Sinha
Uttam Sinha

Reputation: 722

I have created one file named dummy.html under the documents directory. Below is the working code snippet for downloading file. You can log path and see where its pointing. Use safari develop tool iOS simulator for inspecting. I have added file & filetransfer plugin.

function downloadFile() {
window.requestFileSystem(
    LocalFileSystem.PERSISTENT, 0,
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
            "dummy.html", {
                create: true,
                exclusive: false
            },
            function gotFileEntry(fileEntry) {
                console.log(fileEntry);
                var sPath = fileEntry.nativeURL.replace("dummy.html", "");
                console.log(sPath);
                var fileTransfer = new FileTransfer();
                fileEntry.remove();
                fileTransfer.download(
                    "http://developer.android.com/assets/images/home/ics-android.png",
                    sPath + "dummy.png",
                    function(theFile) {
                        console.log("download complete: " + theFile.toURI());
                        showLink(theFile.toURI());
                    },
                    function(error) {
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code: " + error.code);
                    }
                );
            },
            fail);
    },
    fail);

}

Check screenshot-

enter image description here

Upvotes: 2

Related Questions