【问题标题】:How to apply delay in JSZip API to load?如何在 JSZip API 中应用延迟加载?
【发布时间】:2019-01-26 14:09:50
【问题描述】:

如何在 JSZip API 中应用延迟?我想压缩多个文件。但我想在从网络服务器传输文件之间增加一些延迟。如何在下面的代码中添加延迟?

这用于浏览器和在设备内部署二进制文件。

 $scope.download = function () {
            var urls = ["/FILES/AlertLog.txt",
        "/FILES/AuditLog.txt",
        "/FILES/TotLog.txt",
       "/FILES/Historian.csv",
        "/FILES/History2.csv",
        "/FILES/Factory.cfg",
        "/FILES/SLog.txt",
        "/FILES/se.dump",
        "/FILES/AssertLog.txt",
        "/FILES/History2.csv",
        "/FILES/History3.csv",
         "/FILES/History4.csv"
        ];
    compress_files(urls);
}

function compress_files(urls) {
    var zip = new JSZip();
    var deferreds = [];
    for (var i = 0; i < urls.length; i++) {
     $timeout(function(){  deferreds.push(addToZip(zip, urls[i], i));
    },200);// issue to deferreds variable =>undefined
    $.when.apply(window, deferreds).done(generateZip);
}

function addToZip(zip, url, i) {
    var deferred = $.Deferred();
    JSZipUtils.getBinaryContent(url, function (err, data) {
        if(err) {

            deferred.resolve(zip); 
        }
        else {
            var arr=url.split("/");
            zip.file(arr[2], data, { binary: true });
            deferred.resolve(zip);
        }
    });
    return deferred;                                    
}

在从网络服务器传输文件之间添加一些毫秒延迟。

【问题讨论】:

    标签: angularjs eclipse httprequest delay jszip


    【解决方案1】:

    我正在尝试通过延迟来下载单个文件。它解决了我的问题

    function downloadAll(files){
    if(files.length == 0) return;
    file = files.pop();
    var theAnchor = $('<a />')
        .attr('href', file[1])
        .attr('download',file[0])
        // Firefox does not fires click if the link is outside
        // the DOM
        .appendTo('body');
    
    theAnchor[0].click(); 
    theAnchor.remove();
    downloadAll(files); }
    function downloadAll(files){
    if(files.length == 0) return;
    file = files.pop();
    var theAnchor = $('<a />')
        .attr('href', file[1])
        .attr('download',file[0])
        // Firefox does not fires click if the link is outside
        // the DOM
        .appendTo('body');
    
    theAnchor[0].click(); 
    theAnchor.remove();
    downloadAll(files);
    }
    
    $('a.download-csv').on('click', function(){
    downloadAll([
        ['file1.csv', 'data:text/csv;charset=utf8,'+
                  encodeURIComponent('my,csv,file\and,so,on')],
        ['file2.txt', 'data:text/plain;charset=utf8,'+
                  encodeURIComponent('this script can do what I need.')],
        ['file3.js', 'data:text/javascriptcharset=utf8,'+
                  encodeURIComponent('alert(\'You can donate me your house if you like this script :-) \')')]
    ]);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      相关资源
      最近更新 更多