Page 4 of 5

How to download a file using javascript

Posted: Wed Sep 11, 2013 7:24 pm
by Kateryna Grynko

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...


How to download a file using javascript

Posted: Wed Sep 11, 2013 7:51 pm
by Helen

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


How to download a file using javascript

Posted: Wed Sep 11, 2013 8:47 pm
by Kateryna Grynko

Hi Helen,

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


How to download a file using javascript

Posted: Thu Sep 12, 2013 10:54 pm
by Helen

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


How to download a file using javascript

Posted: Fri Sep 13, 2013 10:41 am
by Maryna Brodina

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


How to download a file using javascript

Posted: Tue Sep 17, 2013 8:11 pm
by Helen

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


How to download a file using javascript

Posted: Tue Sep 17, 2013 9:41 pm
by Maryna Brodina

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?


How to download a file using javascript

Posted: Tue Sep 17, 2013 10:55 pm
by Helen

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!


How to download a file using javascript

Posted: Wed Sep 18, 2013 10:13 am
by Kateryna Grynko

Hi Helen,

Do you see any error messages?


How to download a file using javascript

Posted: Wed Sep 18, 2013 3:13 pm
by Helen

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!