Created
April 22, 2012 13:11
-
-
Save nathanpc/2464060 to your computer and use it in GitHub Desktop.
Sample code to download file from internet - Phonegap Wiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<!-- Android | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" /> | |
<meta charset="utf-8">--> | |
<!-- iPad/iPhone specific css below, add after your main css > | |
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="/ipad.css" type="text/css" /> | |
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="/iphone.css" type="text/css" /> | |
--> | |
<!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here --> | |
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> | |
<script type="text/javascript"> | |
// If you want to prevent dragging, uncomment this section | |
/* | |
function preventBehavior(e) | |
{ | |
e.preventDefault(); | |
}; | |
document.addEventListener("touchmove", preventBehavior, false); | |
*/ | |
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch. | |
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html | |
for more details -jm */ | |
/* | |
function handleOpenURL(url) | |
{ | |
// TODO: do something with the url passed in. | |
} | |
*/ | |
function onBodyLoad() | |
{ | |
document.addEventListener("deviceready", onDeviceReady, false); | |
} | |
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("ready"); | |
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); | |
} | |
/* When this function is called, PhoneGap has been initialized and is ready to roll */ | |
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch. | |
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html | |
for more details -jm */ | |
function onDeviceReady() | |
{ | |
// do your thing! | |
downloadFile(); | |
} | |
</script> | |
</head> | |
<body onload="onBodyLoad()"> | |
<h1>Hey, it's PhoneGap!</h1> | |
<p>Don't know how to get started? Check out <em><a target="_blank" href="/http://www.phonegap.com/start">PhoneGap Start</a><em> | |
</p> | |
<br /> | |
<p> | |
DOWNLOADING FILE...<br /> | |
<span id="ready"></span> | |
</p> | |
</body> | |
</html> |
- Download file from url to your device using phonegap
- It is working 3.0 and up to for iOS and android
var folderName = 'xyz';
var fileName;
function downloadFile(URL) {
//step to request a file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem) {
var download_link = encodeURI(URL);
fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL
var directoryEntry = fileSystem.root; // to get root path of directory
directoryEntry.getDirectory(folderName, {
create: true,
exclusive: false
}, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
var rootdir = fileSystem.root;
var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory
fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give
// download function call
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
// Directory created successfuly
}
function onDirectoryFail(error) {
//Error while creating directory
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
//Unable to access file system
alert(evt.target.error.code);
}
}
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
//Download abort errors or download failed errors
alert("download error source " + error.source);
}
);
}
thx parthdevmurari91, it works!
just change "toNativeURL()" deprecated
var fp = fileSystem.root.toNativeURL();
with "toURL()"
var fp = fileSystem.root.toURL();
thax parthdevmurari91
just one question, how do you pass the URL to downloadFile(URL) function
test
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe you'd like to try this:
new FileManager().download_file('http://url','target_path',Log('downloaded success'));
target_path: can include directory (example: dira/dirb/file.html) and the directories will be created recursively.
https://github.com/torrmal/cordova-simplefilemanagement