Forked from nathanpc/phonegap_download_example.html
Last active
August 29, 2015 13:56
-
-
Save claudiojs/9207048 to your computer and use it in GitHub Desktop.
Downloading a file with phonegap.
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> | |
<meta charset="utf-8" /> | |
<meta name="format-detection" content="telephone=no" /> | |
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> | |
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-densitydpi=device-dpi" /> | |
<link rel="stylesheet" type="text/css" href="css/index.css" /> | |
<title>Hello World</title> | |
</head> | |
<body> | |
<div class="app"> | |
<h1>PhoneGap</h1> | |
<div id="deviceready" class="blink"> | |
</div> | |
</div> | |
<script type="text/javascript" src="phonegap.js"></script> | |
<script type="text/javascript"> | |
document.addEventListener('deviceready', onDeviceReady, false); | |
function downloadFile() { | |
console.log('downloadFile'); | |
window.requestFileSystem( | |
LocalFileSystem.PERSISTENT, | |
0, | |
onRequestFileSystemSuccess, | |
fail | |
); | |
} | |
function onRequestFileSystemSuccess(fileSystem) { | |
console.log('onRequestFileSystemSuccess'); | |
fileSystem.root.getFile( | |
'dummy.html', | |
{create: true, exclusive: false}, | |
onGetFileSuccess, | |
fail | |
); | |
} | |
function onGetFileSuccess(fileEntry) { | |
console.log('onGetFileSuccess!'); | |
var path = fileEntry.toURL().replace('dummy.html', ''); | |
var fileTransfer = new FileTransfer(); | |
fileEntry.remove(); | |
fileTransfer.download( | |
'http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf', | |
path + 'theFile.pdf', | |
function(file) { | |
console.log('download complete: ' + file.toURI()); | |
showLink(file.toURI()); | |
}, | |
function(error) { | |
console.log('download error source ' + error.source); | |
console.log('download error target ' + error.target); | |
console.log('upload error code: ' + error.code); | |
} | |
); | |
} | |
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); | |
} | |
/* When this function is called, PhoneGap has been initialized and is ready to roll */ | |
function onDeviceReady() { | |
console.log('device ready!'); | |
downloadFile(); | |
} | |
</script> | |
</body> | |
</html> |
Does anyone know if this will work for iOS and windows phone? I don't have those to test :(
Thanks a lot. It has been days that I was working on file transfer without any success.
Your code works great with 3.5.0 with file-transfer plugin.
It works for me.
However now, having grasped the basics I'm refactoring all the dummy
business into cordova.file.dataDirectory
DOCS: https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a Lot! It took me a day to figure it out, and finally with your code I could understand