Include the given code just above the </head> tag in your index.html . Here we use FileTransfer object and fileTransfer.download() method.
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
alert("Going to start download");
downloadFile();
}
function downloadFile(){
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry){
var sPath = fileEntry.fullPath.replace("dummy.html","");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download(
"http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf",
sPath + "theFile.pdf",
function(theFile) {
console.log("download complete: " + theFile.toURI());
showLink(theFile.toURI());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
);
},
fail);
},
fail);
}
function showLink(url){
alert(url);
var divEl = document.getElementById("deviceready");
var aElem = document.createElement("a");
aElem.setAttribute("target", "_blank");
aElem.setAttribute("href", url);
aElem.appendChild(document.createTextNode("Ready! Click To Open."))
divEl.appendChild(aElem);
}
function fail(evt) {
console.log(evt.target.error.code);
}
</script>
Thanks for the working example. All I had to do was copy and paste and your code worked perfectly.
ReplyDelete:)
ReplyDeleteHi Adarsh, I am completely new to PhoneGAP.
ReplyDeleteYour code look promising, but it's not working for me :(
I made a folder js --> phonegap-1.4.1.js
and index.html containing your code.
Is there more things to add ?? ( like config.html )
Sorry, if I sound stupid, but I have no idea about PhoneGAP
First of all try to use the latest version of PhoneGap 2.9.0. Also please go through some hello world application for better understanding of PhoneGap.
ReplyDeleteAnd please be specific, Are you using a iOS simulator to test your code ? Did you try using alerts to check the flow of code ?
Adarsh thank you for the example. I tried it and it worked on iOS. But, thhe question I have is will the user be able to delete the file that he has downloaded ??? Will he have easy access to the file ??? Because I found the path it gets downloaded to was not accessible in my phone.
ReplyDeleteIn iOS, file will be downloaded to application sandbox. It is not directly accessible by user until the phone is jailbroken.
DeleteIn case of Android, the file is accessible via user as the file system is open to the user.
I am working in cross platform using phonegap. When i tried Download Phonegap API in IOS, file is getting downloaded in phone rather in SD Card So its not safe to save it phone,so Please Suggest me solution So that it support both in Android and ios to open file just to view it,without downloading.....
ReplyDeletehi Pavithra H M,
Deletedo you have a code how to download a file using filetransfer.download in android paltform
This comment has been removed by the author.
DeleteHi Pavithra,
DeleteIf you don't want to let users download the file, you can keep that file in the server and load on demand online.
What type of file you want to open and display ?
Hi Adarsh,
ReplyDeletei am working in cross platform using phonegap. when i tried your code it shows only alert("Going to start download"); its does't not downloading file please help me how to fix this problem
i am using in android it is correct platform
ReplyDeleteIt is supposed to work in Android too. Please try this code in iOS.
DeleteAlso put an alert inside the "function downloadFile(){ " so that we can confirm that method is called.
I have tried your code, it works no problem but when file which is downloaded have size in bytes. and original file which i want to download is of 2.3mb and after download it is of 87 bytes what is this?.You faced problem like this?
ReplyDeleteI tried on my Android phone, and I got "Unfortunately, app has stopped." error.
ReplyDeleteDoes anyone has the working solution for Android?
does this work for video as well????
ReplyDeleteand what if i want to change the name of the file being downloaded???
ReplyDeleteThis code works fine on android but when I used this code on ios, it tells me that the download is successfully completed but I find no files downloaded ... please help
ReplyDeleteHey,
ReplyDeleteVery useful but i want to save image inside img folder of my application. do you have any idea about this???
Hi Sir,when i use your code it give Uncaught ReferenceError : LocalFileSystem is not defined.
ReplyDeleteHi, I have same problem, please help me
DeleteI've tried the code and can`t understand why it isn't working for me. I'm testing directly in my iPad using phonegap build. the only think that works is the "Going to start download" alert after that nothing happens. HELP!!!! PLEASE!!!
ReplyDeleteHi Sir,
ReplyDeleteI am new to Phonegap . Im unable to download a file from the server. i tried your code and i`m getting error code1 ,error in target . Can you please help me out in solving this issue.
Sweet. do you have the html for this?
ReplyDeletehey, i tried using your code without any luck. wheter in the android emulator nor on my phone after installing the APK file.
ReplyDeletei replaced all your console.log commands with an alert because i dont see any console in the emulator not my phone, this is what i receive:
- Going to start download
- download error source http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf
- download error target //theFile.pdf
- upload error code: 1
thats it :(
im stuck all day tryin to produce a "sync/download files app" for android, but no progress so far :(
Yes, it does not work for me too, with the same error messages
Delete