Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save claudiojs/9207048 to your computer and use it in GitHub Desktop.
Save claudiojs/9207048 to your computer and use it in GitHub Desktop.
Downloading a file with phonegap.
<!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>
@claudiojs
Copy link
Author

Updated to PhoneGap 3.3.0

@mjtaljaard
Copy link

Hi. I am trying to incorporate my code into your example, but I am not good ad javascript and unsure how. Gould you give a hand? http://stackoverflow.com/questions/22422535/how-to-download-a-file-from-phonegap-app

@cemerson
Copy link

Is this still working in 3.4.0? I just tried quick test and it's seeming to die after onRequestFileSystemSuccess() ... never gets to onGetFileSuccess().

Strike this - I forgot to add the file-transfer plugin! This works great even with 3.4.0. THANK YOU

@eldafito
Copy link

thanks, the trouble was in fileEntry.fullPath in place of fileEntry.fullPath

@jmrezayi2
Copy link

Thanks a Lot! It took me a day to figure it out, and finally with your code I could understand

@jmrezayi2
Copy link

Does anyone know if this will work for iOS and windows phone? I don't have those to test :(

@FHarth
Copy link

FHarth commented Jun 22, 2014

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.

@stefek99
Copy link

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