Advertisement
Guest User

Asynch DL

a guest
Mar 5th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript" charset="utf-8">
  2.  
  3.     var DATADIR;
  4.     var DATA_URL                        = "http://url_to_file_list/updatedata.php";
  5.     var imagePath           = "http://path_to_files/";
  6.     var imageData           = '';
  7.  
  8. //Loaded file system  
  9. function onFSSuccess(fileSystem) {
  10.     fileSystem.root.getDirectory("appdata",{create:true},gotDir,onError);
  11. }
  12.  
  13.  
  14. //The directory entry callback
  15. function gotDir(d){
  16.     console.log("got dir");
  17.     DATADIR = d;
  18.     var reader = DATADIR.createReader();
  19.     reader.readEntries(function(d){
  20.                        
  21.                        downloadImages();
  22.                        },onError);
  23. }
  24.  
  25.  
  26.  
  27. function onError(e){
  28.     console.log("ERROR");
  29.     console.log(JSON.stringify(e));
  30. }
  31.  
  32. function onDeviceReady() {
  33.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, null);    
  34.    
  35.    
  36. }
  37.  
  38.     function downloadImages(){
  39.        
  40.      $.get(DATA_URL, {}, function(imageList) {
  41.          
  42.            imageData.imageList = imageList;
  43.            console.log("Files : "+imageList);
  44.            console.log("Start downloading...");
  45.            imageData.imagePath = imagePath;
  46.            console.Log('Total images to download: ' + imageData.imageList.length);
  47.            downloadImage();
  48.       }, "json");
  49.        
  50.     }
  51.    
  52.     /* download image syncronously */
  53.     function downloadImage(){
  54.         if(imageData.imageList.length > 0){
  55.             var imageName = imageData.imageList[0];
  56.             var imagePath = imageData.imagePath;
  57.            
  58.             console.Log('Start file download: ' + imagePath + imageName);
  59.             //check if file exists, pass the function as new closure, so the variable is preserved
  60.             DATADIR.getFile(imageName, {create: false}, successImageExists, (function(name, path){
  61.                                                                               return function(){
  62.                                                                               failImageExists(name, path)
  63.                                                                               }
  64.                                                                               })(imageName, imagePath));
  65.         }
  66.     }
  67.    
  68.     /* image exists */
  69.     function successImageExists(file){
  70.         console.Log('Image Exists: ' + file.name);
  71.         updateImageDownloadProgress();
  72.     }
  73.    
  74.     /* image doesn't exists */
  75.     function failImageExists(imageName,imagePath){
  76.         console.Log('image does not Exists');  
  77.         //start download  
  78.         console.Log('Download Image To: ' + DATADIR.fullPath + '/' + imageName);
  79.         ft.download(imagePath + escape(imageName), DATADIR.fullPath + '/' + imageName, onImageDownload, onImageDownloadFail);
  80.     }
  81.    
  82.     /* on image download fail */
  83.     function onImageDownloadFail(error){
  84.         console.Log('image not downloaded. Error: ' + JSON.stringify(error));
  85.         updateImageDownloadProgress();
  86.     }
  87.    
  88.     /* on image download success */
  89.     function onImageDownload(image){
  90.         updateImageDownloadProgress();
  91.     }
  92.    
  93.     /* update image download progress */
  94.     function updateImageDownloadProgress(){
  95.         imageData.imageList = imageData.imageList.slice(1);
  96.         console.Log('images left to download: ' + imageData.imageList.length);
  97.         if(imageData.imageList.length == 0){
  98.             imagesDownloaded = true;
  99.         } else {
  100.             downloadImage();
  101.         }
  102.     }
  103.  
  104.  
  105. function init() {
  106.     //document.location='app.html';
  107.     document.addEventListener("deviceready", onDeviceReady, true);
  108. }
  109. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement