【问题标题】:Adding Multiple Files into a Zip File not Working将多个文件添加到 Zip 文件中不起作用
【发布时间】:2020-01-17 02:14:37
【问题描述】:

我正在使用 JSZip 将选定的文件添加到单个 zip 文件中。 问题只是最后选择的文件被添加到 zip 中(也已损坏),请参见下面的代码:

var zip = new JSZip();
var items = '';
var count = 0;
var zipName = 'resources.zip';

$$('input[type=checkbox]').each(function(e){

    if(e.checked){
        if(e.id!='select-all'){
            items = items + "'" + e.getAttribute('data-href') + "'|" + e.getAttribute('data-file') + ",";
        }
    }
});

if(items!=''){

    items = items.slice(0,-1)
    var tmp = items.split(',');

    for(i=0;i<tmp.length;i++){
        var item = tmp[i].split('|');
        var url = item[0];
        var filename = item[1];

        JSZipUtils.getBinaryContent(url, function (err, data) {
            if(err) {
                throw err; // or handle the error
            }
            zip.file(filename, data, {binary:true});
            count++;

            if(count == tmp.length) {

                zip.generateAsync({type:'blob'}).then(function(content) {
                    saveAs(content, zipName);
                });

            }

        });
    }

}

谁能看到我的代码的问题?

【问题讨论】:

  • 另外,zip 中的文件已损坏,表明下载文件时出现问题。

标签: javascript zip jszip


【解决方案1】:

修复它。问题是我没有将网址正确放入数组中。

修正线:

items.push(e.getAttribute('data-href') + '|' + e.getAttribute('data-file'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多