我怎么知道下载完成后?

时间:2012-06-15 13:38:26

标签: jquery-mobile cordova

今天我的问题是要知道是否有办法知道何时完成了使用查询移动设备下载到本地设备的某些文件。 这是我在for循环中插入的代码

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";

我的问题是,使用此代码,它可以正确下载文件,但在我下载时,它会转到settori.html页面。 我需要等待下载所有文件,然后转到settori.html

3 个答案:

答案 0 :(得分:1)

尝试使用此代码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";
    }
);    

答案 1 :(得分:0)

就这段代码而言:

$(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";

使用此代码,应用程序在下载文件时转到index.html。我需要确保在转到index.html时下载所有文件。

答案 2 :(得分:0)

试试这个

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);
}

这可能在某种程度上对你有帮助。

相关问题