Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript" charset="utf-8">
- var DATADIR;
- var DATA_URL = "http://url_to_file_list/updatedata.php";
- var imagePath = "http://path_to_files/";
- var imageData = '';
- //Loaded file system
- function onFSSuccess(fileSystem) {
- fileSystem.root.getDirectory("appdata",{create:true},gotDir,onError);
- }
- //The directory entry callback
- function gotDir(d){
- console.log("got dir");
- DATADIR = d;
- var reader = DATADIR.createReader();
- reader.readEntries(function(d){
- downloadImages();
- },onError);
- }
- function onError(e){
- console.log("ERROR");
- console.log(JSON.stringify(e));
- }
- function onDeviceReady() {
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, null);
- }
- function downloadImages(){
- $.get(DATA_URL, {}, function(imageList) {
- imageData.imageList = imageList;
- console.log("Files : "+imageList);
- console.log("Start downloading...");
- imageData.imagePath = imagePath;
- console.Log('Total images to download: ' + imageData.imageList.length);
- downloadImage();
- }, "json");
- }
- /* download image syncronously */
- function downloadImage(){
- if(imageData.imageList.length > 0){
- var imageName = imageData.imageList[0];
- var imagePath = imageData.imagePath;
- console.Log('Start file download: ' + imagePath + imageName);
- //check if file exists, pass the function as new closure, so the variable is preserved
- DATADIR.getFile(imageName, {create: false}, successImageExists, (function(name, path){
- return function(){
- failImageExists(name, path)
- }
- })(imageName, imagePath));
- }
- }
- /* image exists */
- function successImageExists(file){
- console.Log('Image Exists: ' + file.name);
- updateImageDownloadProgress();
- }
- /* image doesn't exists */
- function failImageExists(imageName,imagePath){
- console.Log('image does not Exists');
- //start download
- console.Log('Download Image To: ' + DATADIR.fullPath + '/' + imageName);
- ft.download(imagePath + escape(imageName), DATADIR.fullPath + '/' + 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(){
- imageData.imageList = imageData.imageList.slice(1);
- console.Log('images left to download: ' + imageData.imageList.length);
- if(imageData.imageList.length == 0){
- imagesDownloaded = true;
- } else {
- downloadImage();
- }
- }
- function init() {
- //document.location='app.html';
- document.addEventListener("deviceready", onDeviceReady, true);
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement