Corallino
Corallino

Reputation: 109

how can i know when download is finished?

today my problem is to know if there is a way to know when is finished the download to local device of some files with query mobile. This is my code that is insert in a for cycle

for(var id=0; id<100; id++){
    var ft = new FileTransfer();
    var dlPath = DATADIR.fullPath + "/" +id+".jpg";
    ft.download(reconstitutedObject['immagine'], dlPath, function(e){
    }, onError);
 }
 window.location.href="settori.html";

My problem is that with this code, it make correctly the download of files, but while i'm downloading, it goes to settori.html page. I need to wait that all files are downloaded and than go to settori.html

Upvotes: 0

Views: 629

Answers (3)

Pradip Kharbuja
Pradip Kharbuja

Reputation: 3532

try this

for(var id=0; id<100; id++){
  var ft = new FileTransfer();
  var dlPath = DATADIR.fullPath + "/" +id+".jpg";
  ft.download(reconstitutedObject['immagine'], dlPath, function(e){
    if(id == 99) {
      window.location.href="settori.html";
    }
 }, onError);
}

This may help you to some extent.

Upvotes: 0

Corallino
Corallino

Reputation: 109

And in the case of this code:

$(data).find("vettura").each(function () {
        var id=$(this).attr("id");
        var immag=$(this).find("immagine").first().find("grande").text();

        if(immag!=""){
              var ft = new FileTransfer();
              var dlPath = DATADIR.fullPath + "/" +id+".jpg";
              ft.download(immag, dlPath, function(e){
                      //renderPicture(e.fullPath);
                      console.log("Successful download of "+e.fullPath);
               }, onError);                                                                 
         }
 });               

 window.location.href="index.html";

With this code, the app goes to index.html while file are downloading. I need to be sure that when go to index.html all files are downloaded.

Upvotes: 0

dhaval
dhaval

Reputation: 7659

Try with this code with async.js:

var i = 0;
async.whilst(
    function () { return count < 100; },
    function (callback) {
        count++;
        var ft = new FileTransfer();
        var dlPath = DATADIR.fullPath + "/" +id+".jpg";
        ft.download(reconstitutedObject['immagine'], dlPath, function(entry){
           console.log("File downloaded to " + entry.fullPath);
           callback(); // resume to next download    
        }, function(err){
           console.log("download error");
           callback(); // resume to next download
        });
    },
    function (err) {
        // all download complete
        window.location.href="settori.html";
    }
);    

Upvotes: 1

Related Questions