Guest User

PhoneGap iOS synchronous downloading

a guest
May 17th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var thumbsToDownload = [];
  2. var statusDom;
  3. var remoteFilePath;
  4. var theArray=['domain.com/image1.jpg','domain.com/image2.jpg'];  //the array of remote urls to download
  5. function downloadAllTheStuff() {
  6.    
  7.     //cycle through to download each image syncronously
  8.    
  9.     for (var i=0; i<vidArray.length; i++){
  10.         //array of thumbnails to download
  11.         thumbsToDownload.push(theArray[i]);
  12.     }
  13.     console.log('Total number of thumbnails to download: '+thumbsToDownload.length);
  14.    
  15.     downloadThumbnail();
  16.    
  17. }
  18. /* download image syncronously */
  19. var imagePath;
  20. var localPath;
  21. var imageName;
  22. var imagesDownloaded;
  23.  
  24. function downloadThumbnail(){
  25.    
  26.     if(thumbsToDownload.length > 0){
  27.    
  28.         imageName = thumbsToDownload[0].substring(remoteFilePath.lastIndexOf('/')+1);
  29.         imagePath = "domain.com/location/of/remote/files/";
  30.  
  31.         //console.log('The image to download is here: '+imagePath);
  32.         console.log('Start file download: ' + imageName);
  33.         //check if file exists, pass the function as new closure, so the variable is preserved
  34.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSForDL, fail);
  35.     }
  36. }
  37.  
  38. function gotFSForDL(fileSystem){
  39.     console.log('got the fs');
  40.     //fileSystem.root.getFile(imageName, {create: true, exclusive: false}, gotFileEntryDL, fail);
  41.     window.rootFS = fileSystem.root;
  42.     //console.log(window.rootFS);
  43.     var failFS = failImageExists(imageName,window.rootFS.fullPath);
  44.    
  45.     window.rootFS.getFile(imageName, {create: false}, successImageExists, failFS);
  46. }
  47.  
  48.  
  49. /* image exists */
  50. function successImageExists(file){
  51.     console.log('Image Exists: ' + file.name);
  52.     console.log('Image Exists at: ' + file.fullPath);
  53.     localPath = file.fullPath;
  54.     updateImageDownloadProgress();
  55. }
  56.  
  57. /* image doesn't exists */
  58. function failImageExists(imageName,localPathParam){
  59.     console.log('image does not exist');
  60.     //start download
  61.     console.log('Download Image To: ' + imageName);
  62.     var ft = new FileTransfer();
  63.     ft.download(imagePath+imageName, localPathParam+"/"+imageName, onImageDownload, onImageDownloadFail);
  64. }
  65.  
  66. /* on image download fail */
  67. function onImageDownloadFail(error){
  68.     console.log('image not downloaded. Error: ' + JSON.stringify(error));
  69.     updateImageDownloadProgress();
  70. }
  71.  
  72. /* on image download success */
  73. function onImageDownload(image){
  74.     updateImageDownloadProgress();
  75. }
  76.  
  77. /* update image download progress */
  78. function updateImageDownloadProgress(){
  79.     thumbsToDownload = thumbsToDownload.slice(1);
  80.     console.log('update');
  81.     console.log('images left to download: ' + thumbsToDownload.length);
  82.     if(thumbsToDownload.length == 0){
  83.         imagesDownloaded = 'true';
  84.     } else {
  85.         downloadThumbnail();
  86.     }
  87.    
  88. }
Add Comment
Please, Sign In to add comment