jquery fileDownload:将多个文件下载到块中

时间:2017-03-20 13:08:26

标签: javascript jquery download

我正在使用https://github.com/johnculviner/jquery.fileDownload下载文件数组。它似乎有效:

  for (var index = 0; index < files.length; ++index) {
    var file = files[index];
    $.fileDownload(file)
      .done(function() {
        alert("Done " + file);
      }
    )
      .fail(function() {
        alert("Fail " + file);
      }
    );
  }

如何一次下载5个文件的文件? 文件正在下载,但暂时没有显示警报。所以我想也许我在这里遇到了问题。

例如,当我尝试下载100个文件时,我想下载5个,然后是其他5个等等,直到完成所有操作。这可能吗?

1 个答案:

答案 0 :(得分:1)

下载第一个文件

var firstfile = files[0]; //first file id
var downloadresult = downloadFileEx(firstfile);

然后使用jquery then 链接其他文件,并使用array.slice选择块

var begin=1; // start from second file 
var end=5;

do {

for (var index = begin; index < files.slice(begin, end).length; i++) {
 var file = files[index];
(function (index) {
      downloadresult = downloadresult.then(function() {
        return downloadFileEx(array[index]);
       });
   }(index));
 }
index++;
 }
begin = begin +5; 
end = end + 5;
}
while (end  < files.length);

编写文件下载功能

function downloadFileEx(file) {
 return $.fileDownload(file)
      .done(function() {
        alert("Done " + file); // remove this 100 alerts will be blocked by browser, maintain an array of files
      }
    )
      .fail(function() {
        alert("Fail " + file);
      }
    );
}