Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var thumbsToDownload = [];
- var statusDom;
- var remoteFilePath;
- var theArray=['domain.com/image1.jpg','domain.com/image2.jpg']; //the array of remote urls to download
- function downloadAllTheStuff() {
- //cycle through to download each image syncronously
- for (var i=0; i<vidArray.length; i++){
- //array of thumbnails to download
- thumbsToDownload.push(theArray[i]);
- }
- console.log('Total number of thumbnails to download: '+thumbsToDownload.length);
- downloadThumbnail();
- }
- /* download image syncronously */
- var imagePath;
- var localPath;
- var imageName;
- var imagesDownloaded;
- function downloadThumbnail(){
- if(thumbsToDownload.length > 0){
- imageName = thumbsToDownload[0].substring(remoteFilePath.lastIndexOf('/')+1);
- imagePath = "domain.com/location/of/remote/files/";
- //console.log('The image to download is here: '+imagePath);
- console.log('Start file download: ' + imageName);
- //check if file exists, pass the function as new closure, so the variable is preserved
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSForDL, fail);
- }
- }
- function gotFSForDL(fileSystem){
- console.log('got the fs');
- //fileSystem.root.getFile(imageName, {create: true, exclusive: false}, gotFileEntryDL, fail);
- window.rootFS = fileSystem.root;
- //console.log(window.rootFS);
- var failFS = failImageExists(imageName,window.rootFS.fullPath);
- window.rootFS.getFile(imageName, {create: false}, successImageExists, failFS);
- }
- /* image exists */
- function successImageExists(file){
- console.log('Image Exists: ' + file.name);
- console.log('Image Exists at: ' + file.fullPath);
- localPath = file.fullPath;
- updateImageDownloadProgress();
- }
- /* image doesn't exists */
- function failImageExists(imageName,localPathParam){
- console.log('image does not exist');
- //start download
- console.log('Download Image To: ' + imageName);
- var ft = new FileTransfer();
- ft.download(imagePath+imageName, localPathParam+"/"+imageName, onImageDownload, onImageDownloadFail);
- }
- /* on image download fail */
- function onImageDownloadFail(error){
- console.log('image not downloaded. Error: ' + JSON.stringify(error));
- updateImageDownloadProgress();
- }
- /* on image download success */
- function onImageDownload(image){
- updateImageDownloadProgress();
- }
- /* update image download progress */
- function updateImageDownloadProgress(){
- thumbsToDownload = thumbsToDownload.slice(1);
- console.log('update');
- console.log('images left to download: ' + thumbsToDownload.length);
- if(thumbsToDownload.length == 0){
- imagesDownloaded = 'true';
- } else {
- downloadThumbnail();
- }
- }
Add Comment
Please, Sign In to add comment