0
votes

I am new to PhoneGap programming.My question is

how to store downloaded PDF file internally with in the application for iOS. Here is what I have:

function downloadFile() {
    alert('start');
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
        window.resolveLocalFileSystemURI("http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf", onResolveSuccess, fail);
    }

function onFileSystemSuccess(fileSystem) {
    alert(fileSystem.name);
    console.log(fileSystem.name);
}

function onResolveSuccess(fileEntry) {
    alert('success');
    console.log(fileEntry.name);
}

function fail(evt) {
    console.log(evt.target.error.code);
}

And also I added plugin as cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git

But it is not working.Do we need any plugin for this LocalSystem?Please help me.

1
How to set target path to save downloaded file with in the app - rani
For ios u can get the Document directory path using [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; and then append your file name to it. this will save it in the documents directory - Prathamesh Saraf
how to use it in PhoneGap project? - rani
@rani check this one stackoverflow.com/questions/6417055/… i got success in android not tested in ios. - Aravin

1 Answers

0
votes

ok this is the entire code to download a file to documents directory for ios

document.addEventListener('deviceready',onDeviceReady, false);

function onDeviceReady()
{
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,onFileSystemSuccess, fail);
}

function fail() {
    console.log("failed to get filesystem");
}

function onFileSystemSuccess(fileSystem) {
    var fileTransfer = new FileTransfer();
    var uri = encodeURI("http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf");

    fileTransfer.download(
          uri,
          fileSystem.root.fullPath+"/MyPdf.pdf",
          function(entry) {
            console.log("download complete: " + entry.fullPath);
          },
          function(error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
          }
      );
}

this will download the Nitobi.pdf file to documents directory as MyPdf.pdf

For reference.

*******EDIT*********

For phonegap version 3.4 the fileSystem.root.fullPath+"/MyPdf.pdf", needs to be replaced with cdvfile://localhost/persistent/MyPdf.pdf

Courtesy of Phonegap 3.4 FileTransfer error (iOS)