Hamza Belheni
Hamza Belheni

Reputation: 45

Using File Transfer Plugin in Phonegap Application

I have more than 2 weeks that i tried to download file PDF in phonegap application using the File Transfer Plugin but it didn't work!! i make everything for that: -installing the last version of phonegap -installing the last version of File Transfer plugin And this is the code to integrate in Javascript interface:

var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "file://sdcard/ics-android.png",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });

But it seems wrong!!! I have like a result last three alerts in an android device: -Download error source -Download error target -Upload error code What i should doing?!!

Upvotes: 0

Views: 673

Answers (2)

Gandhi
Gandhi

Reputation: 11935

Request you to check out my github page that contains a sample Cordova app which downloads PDF file from external URL and downloads it to the device.

This sample app is tested both in iOS and android devices. Hope it helps.

Upvotes: 1

Nikita Kunevich
Nikita Kunevich

Reputation: 597

According to file transfer plugin documentation, first of all you need to create a file where you will store your remote data. Your code should look like this:

{
    //call this after onDeviceReady event
    ...
    var savePath = cordova.file.externalRootDirectory;
    var fileName = "ics-android.png";
    var url = encodeURI("http://developer.android.com/assets/images/home/ics-android.png");

    downloadFile(savePath, fileName, url);
    ...
}



function downloadFile(savePath, fileName, remoteURL) {
    window.resolveLocalFileSystemURL(savePath, function (dirEntry) {
        console.log('file system open: ' + dirEntry.name);
        createFile(dirEntry, fileName, function (fileEntry) {
            download(remoteURL, fileEntry);
        });
    }, function (err) { alert(err) });
}

function createFile(dirEntry, fileName, callback) {
    // Creates a new file or returns the file if it already exists.
    dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
        callback(fileEntry);
    }, function (err) { alert(err) });

}
function download(remoteURL, fileEntry) {
    var fileURL = fileEntry.toURL();
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        remoteURL,
        fileURL,
        function (entry) {
            alert("download complete: " + entry.fullPath);
        },
        function (error) {
            alert("download error source " + error.source);
            alert("download error target " + error.target);
            alert("upload error code" + error.code);
        });
}

Note that for path I use cordova.file.externalRootDirectory, so you get root sdcard path for file.

Upvotes: 0

Related Questions