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...
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
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...
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
Hi Helen,
This code won't work on preview, this is for device only.
Is the path "file://data/" available on your device?
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
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
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
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?
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!
Hi Helen,
Do you see any error messages?
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!