Daniel Euchar
Daniel Euchar

Reputation: 1820

download to local storage android phonegap 3.4.0

I have this code below for downloading a jpg into my local storage but i'm not getting any file downloaded i have the following plugins installed

after this i ran phonegap build android and installed my apk in my device didn't receive any alerts. kindly help

Upvotes: 0

Views: 535

Answers (1)

Gajotres
Gajotres

Reputation: 57309

You must run your code when deviceReadyevent triggers:

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
        //Your code goes here
}

It should look like this:

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
    var url = 'http://2d-code.co.uk/images/google-qr-code.jpg';
    var filePath = 'file:///android_asset/www/res/db/"';
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);

    fileTransfer.download(
        uri,
        filePath,
        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);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}

Read more about it here.

Upvotes: 1

Related Questions