Created
July 5, 2012 17:59
-
-
Save dhavaln/3055240 to your computer and use it in GitHub Desktop.
Cordova (1.7.0, 1.8.1 and 1.9.0) File Download Test
This file contains hidden or 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
<script type="text/javascript" src="lib/android/cordova-1.7.0.js"></script> | |
<script type="text/javascript"> | |
window.appRootDirName = "download_test"; | |
document.addEventListener("deviceready", onDeviceReady, false); | |
function onDeviceReady() { | |
console.log("device is ready"); | |
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); | |
} | |
function fail() { | |
console.log("failed to get filesystem"); | |
} | |
function gotFS(fileSystem) { | |
console.log("filesystem got"); | |
window.fileSystem = fileSystem; | |
fileSystem.root.getDirectory(window.appRootDirName, { | |
create : true, | |
exclusive : false | |
}, dirReady, fail); | |
} | |
function dirReady(entry) { | |
window.appRootDir = entry; | |
console.log("application dir is ready"); | |
} | |
downloadFile = function(){ | |
var fileTransfer = new FileTransfer(); | |
var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf"; | |
var filePath = window.appRootDir.fullPath + "/test.pdf"; | |
fileTransfer.download( | |
url, | |
filePath, | |
function(entry) { | |
alert("download complete: " + entry.fullPath); | |
}, | |
function(error) { | |
alert("download error" + error.source); | |
} | |
); | |
} | |
</script> | |
</head> | |
<body> | |
<a href="#" onclick="downloadFile()">Download File</a> | |
</body> | |
</html> |
Where is that FileTransfer class coming from? Is that JS or native code?
D'oh. Please ignore last. (It's an object in the File API that I just hadn't noticed before.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My bad. Works like a charm.
Just one minor thing tho': I dont see a reason why a folder has to be created again, given that the files are already stored inside the sandboxed application folder.