Skip to content

Instantly share code, notes, and snippets.

@valerykalashnikov
Last active January 4, 2016 13:39
Show Gist options
  • Save valerykalashnikov/8629528 to your computer and use it in GitHub Desktop.
Save valerykalashnikov/8629528 to your computer and use it in GitHub Desktop.
Example how to download files via http using phonegap
<!DOCTYPE html>
<html>
<head>
<!-- Set the viewport to show the page at a scale of 1.0, and make it non-scalable -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<script src="cordova.js"></script>
</head>
<body>
<script>
function downloadFile(){
//to get full path to root of file system make this
window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,
onFileSystemSuccess,
fail
);
}
function onFileSystemSuccess(fileSystem) {
//get the full path
var sPath = fileSystem.root.fullPath;
console.log(sPath);
//to download file instantiating FileTransfer object.
//Don't forget to install this plugin
var fileTransfer = new FileTransfer();
//Download file
fileTransfer.download(
//from here
"http://www.w3.org/2011/web-apps-ws/papers/Sencha.html",
//and save it here
sPath + "/sencha.html",
onFileTransferSuccess,
onFileTransferError
);
}
function onFileTransferSuccess(file) {
//we are win! File on our device
console.log("download complete: " + file.toURI());
}
function onFileTransferError(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
}
function fail(evt) {
alert(evt.target.error.code);
}
</script>
<!-- Let's start -->
<script type="text/javascript">
function onDeviceReady() {
downloadFile();
}
document.addEventListener('deviceready', onDeviceReady, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment