Skip to content

Instantly share code, notes, and snippets.

@macdonst
Created October 4, 2012 17:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save macdonst/3835045 to your computer and use it in GitHub Desktop.
Save macdonst/3835045 to your computer and use it in GitHub Desktop.
FileTrasfer a bunch of files.
var remoteFiles = [];
function downloadRemotePDF() {
var local2User = JSON.parse( localStorage["locallessons"] );
$.each(local2User, function(key) {
remoteFiles.push(optionsJSON + local2User[key].idcountries + '/' + local2User[key].idcurriculum + '/' + local2User[key].idoptions + '/pdf/' + local2User[key].pdfname);
}
downloadFile();
}
function downloadFile() {
// No files left, stop downloading
if (remoteFiles.length == 0) {
return;
}
var remoteFile = remoteFiles.pop();
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, function(fileEntry) {
var localPath = fileEntry.fullPath;
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
localPath = localPath.substring(7);
}
var ft = new FileTransfer();
ft.download(remoteFile, localPath, function(entry) {
// Do what you want with successful file downloaded and then
// call the method again to get the next file
downloadFile();
}, fail);
}, fail);
}, fail);
}
@gnnpls
Copy link

gnnpls commented May 26, 2013

you saved my day friend...
two days i was handling with downloadin multiple files...
Your idea showed me the solution..
Thanx a lot

@coomsie
Copy link

coomsie commented Jan 27, 2014

Gidday,

Thanks man ... same as above. :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment