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

How to download a file using javascript

Hello! You need to define filePath. For example after codevar fileTransfer = new FileTransfer();/code add codevar filePath = "file://sdcard/XXXXX.pdf&quot/code where file:/// - path you use to save
XXXXX.pdf - name you use to save
Please note it wont't work in browser as it's PhoneGap feature, you would need to export and install app on device first.

Nayef
Posts: 0
Joined: Mon Jun 17, 2013 4:08 pm

How to download a file using javascript

1- Can i use appery.io Tester to test the app on device ?

2- Doesn't work see the code :

var fileTransfer = new FileTransfer();
var filePath = "file://sdcard/quickstart.pdf";
var uri = encodeURI("http://www.example.com/uploads/quicks...");

fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);

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

How to download a file using javascript

Yes, but some PhoneGap features might not work in Tester. If it doesn't work it tester then please try to install app on device. Please let us know about result.

Nayef
Posts: 0
Joined: Mon Jun 17, 2013 4:08 pm

How to download a file using javascript

Not working after install it

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

How to download a file using javascript

Hi,

What are the errors in your app as a result?
Are you sure in header basic auth: "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" ?

Nayef
Posts: 0
Joined: Mon Jun 17, 2013 4:08 pm

How to download a file using javascript

Hi,

1st : The result is nothing
2nd : I don't know what is Authorization and how i use it in this case .

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

How to download a file using javascript

Please replace codeconsole.log/codewithcodealert/codeand show us the result.

Also please find out if http-authorization is needed on site where you download file from.

Nayef
Posts: 0
Joined: Mon Jun 17, 2013 4:08 pm

How to download a file using javascript

The result is :

After click the button
Popup window :

Title: Alert
ـــــــــــــــــــــــــــــــــــــــــــــــــــــ
download error source
http://www.mysite.com
/uploads/quickstart.pdf

____________________

ok

------------------ After Click ok
Title: Alert
ـــــــــــــــــــــــــــــــــــــــــــــــــــــ
download error target
file://sdcard/quickstart.pdf

____________________

ok

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

____________________

ok

------------------ After Click 3rd ok nothing happened

The code used :

var fileTransfer = new FileTransfer();
var filePath = "file://sdcard/quickstart.pdf";
var uri = encodeURI("http://www.mysite.com/uploads/quickst...");

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=="
}
}
);

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

How to download a file using javascript

Hi,

1) let's check first if your app downloads any file. Try to download some PDF file when you don't need authorization, for example http://samplepdf.com/sample.pdf

2) please ensure you use correct login and password. Are you able to retrieve your PDF file using curl command? If so could you clarify what curl request parameters you use?

Nayef
Posts: 0
Joined: Mon Jun 17, 2013 4:08 pm

How to download a file using javascript

I replaced the link and it's working now

Only : Onclick navgate to link

The problem is from my site ):

Return to “Issues”