Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* download all the images */
- function downloadImages(data){
- var imageList = data.IMAGELIST;
- var imagePath = data.IMAGEPATH;
- var imageName = "";
- imageData.imagePath = imagePath;
- imageData.imageList = [];
- var colMap = new Object();
- //create column map
- for (var i = 0; i < imageList.COLUMNS.length; i++) {
- colMap[imageList.COLUMNS[i]] = i;
- }
- for (var i = 0; i < imageList.DATA.length; i++) {
- imageName = imageList.DATA[i][colMap["NAME"]];
- imageData.imageList.push(imageName);
- }
- consoleLog('Total images to download: ' + imageData.imageList.length);
- downloadImage();
- }
- /* download image syncronously */
- function downloadImage(){
- if(imageData.imageList.length > 0){
- var imageName = imageData.imageList[0];
- var imagePath = imageData.imagePath;
- consoleLog('Start file download: ' + imagePath + imageName);
- //check if file exists, pass the function as new closure, so the variable is preserved
- imageDir.getFile(imageName, {create: false}, successImageExists, (function(name, path){
- return function(){
- failImageExists(name, path)
- }
- })(imageName, imagePath));
- }
- }
- /* image exists */
- function successImageExists(file){
- consoleLog('Image Exists: ' + file.name);
- updateImageDownloadProgress();
- }
- /* image doesn't exists */
- function failImageExists(imageName,imagePath){
- consoleLog('image does not Exists');
- //start download
- consoleLog('Download Image To: ' + imageDir.fullPath + '/' + imageName);
- ft.download(imagePath + escape(imageName), imageDir.fullPath + '/' + imageName, onImageDownload, onImageDownloadFail);
- }
- /* on image download fail */
- function onImageDownloadFail(error){
- consoleLog('image not downloaded. Error: ' + JSON.stringify(error));
- updateImageDownloadProgress();
- }
- /* on image download success */
- function onImageDownload(image){
- updateImageDownloadProgress();
- }
- /* update image download progress */
- function updateImageDownloadProgress(){
- imageData.imageList = imageData.imageList.slice(1);
- consoleLog('images left to download: ' + imageData.imageList.length);
- if(imageData.imageList.length == 0){
- imagesDownloaded = true;
- } else {
- downloadImage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement