0

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.

Mathias
  • 5,642
  • 2
  • 31
  • 46
rani
  • 593
  • 3
  • 10
  • 28
  • please refer this question http://stackoverflow.com/questions/6417055/download-files-and-store-them-locally-with-phonegap-jquery-mobile-android-and-io – Prathamesh Saraf May 15 '14 at 08:32
  • How to set target path to save downloaded file with in the app – rani May 15 '14 at 09:11
  • 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 May 15 '14 at 09:38
  • how to use it in PhoneGap project? – rani May 15 '14 at 10:04
  • @rani check this one http://stackoverflow.com/questions/6417055/download-files-and-store-them-locally-with-phonegap-jquery-mobile-android-and-io i got success in android not tested in ios. – Aravin May 15 '14 at 10:09
  • Aravin,I tried in iOS,it is giving error like File Transfer Error: Could not create target file – rani May 15 '14 at 10:39

1 Answers1

0

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)

Community
  • 1
  • 1
Prathamesh Saraf
  • 709
  • 4
  • 14
  • ,I tried as per this but It is giving error as FileTransferError { body = "Could not create target file"; code = 1; "http_status" = 200; source = "http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf"; target = "cdvfile://localhost/root/MyPdf.pdf"; } – rani May 15 '14 at 11:07
  • are u using phonegap 3.4? then the solution can be founf here http://stackoverflow.com/questions/23097188/phonegap-3-4-filetransfer-error-ios – Prathamesh Saraf May 15 '14 at 11:13
  • you want to open it within the app? – Prathamesh Saraf May 15 '14 at 11:46
  • i am not sure for phonegap since i never had to do it before, for iOS native you can use a webview to do it. You can google or ask a separate question for it. – Prathamesh Saraf May 15 '14 at 11:52