Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to download a file using javascript

Hi Helen,

Remove header for authorization please.
It's generated based on user name and password so you can't just copy it from another app.
Read more here: http://docs.phonegap.com/en/2.4.0/cor...

Helen
Posts: 0
Joined: Wed Aug 28, 2013 8:35 pm

How to download a file using javascript

Hi,
I'm not sure where you're referring to regarding header authorization.
Here's my sample code:
pre
//alert('Device ready, download to follow');

function successDownload(entry) {
alert("download complete: " + entry.fullPath);
}

function failDownload(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}

var fileTransfer = new FileTransfer();
alert('FileTransfer instantiated.');

var uri = encodeURI("http://www.goldbeltbyway.com/sites/default/files/uploads/shelf1.jpg");
var filePath = 'file://data/shelf1.jpg';

fileTransfer.download(uri, filePath, successDownload, failDownload);

/pre

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to download a file using javascript

Hi Helen,

This code won't work on preview, this is for device only.
Is the path "file://data/" available on your device?

Helen
Posts: 0
Joined: Wed Aug 28, 2013 8:35 pm

How to download a file using javascript

Hi Katya,
Yep, i've installed the .apk on Android phone, also tried viewing using Appery Test app for android.
Yes, path /data is available.
I get some 2 errors noted above about 'download error source', ,download error target....

Thanks,
Helen

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to download a file using javascript

Hello! In JS above there is incorrect path to the file:
codevar filePath = 'file://data/shelf1.jpg';/code
should be
codevar filePath = 'file://sdcard/data/shelf1.jpg';/code

Helen
Posts: 0
Joined: Wed Aug 28, 2013 8:35 pm

How to download a file using javascript

Hi Marina,

I added 'sdcard' to filePath above, I get the same errors.
I tested using both Appery Test App and install .apk to device.

Thnx

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to download a file using javascript

Hi, this code works for us if install .apk on device:
codefunction successDownload(entry) {
alert("download complete: " + entry.fullPath);
}
function failDownload(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
var fileTransfer = new FileTransfer();
alert('FileTransfer instantiated.');
var uri = encodeURI("http://www.goldbeltbyway.com/sites/default/files/uploads/shelf1.jpg");
var filePath = 'file://sdcard/data/shelf1.jpg';
fileTransfer.download(uri, filePath, successDownload, failDownload);/code
Please clarify what is the error you get and what device/version you use?

Helen
Posts: 0
Joined: Wed Aug 28, 2013 8:35 pm

How to download a file using javascript

Thanks Marina, however it's still not working for me.

I installed .apk on Motorola Droid Bionic, version 4.1.2.

Code is now in onclick event of a button... does this make a difference?

Thanks!

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to download a file using javascript

Hi Helen,

Do you see any error messages?

Helen
Posts: 0
Joined: Wed Aug 28, 2013 8:35 pm

How to download a file using javascript

Yes, same errors as mentioned above.

Title: Alert
ـــــــــــــــــــــــــــــــــــــــــــــــــــــ
download error source
http://www.goldbeltbyway.com/sites/de...

____________________

ok

------------------ After Click ok
Title: Alert
ـــــــــــــــــــــــــــــــــــــــــــــــــــــ
download error target
file://sdcard/data/shelf1.jpg

____________________

ok

------------------ After Click 2nd ok
Title: Alert
ـــــــــــــــــــــــــــــــــــــــــــــــــــــ
upload error code: 3

____________________

ok

I realize code 3 is connection error. I'm not so familiar with mobile devices, I checked settings that would perhaps limit downloads, but do not notice anything obvious.

Thanks!

Return to “Issues”