无法将多个文件添加到Zip文件中 [英] Adding Multiple Files into a Zip File not Working

查看:173
本文介绍了无法将多个文件添加到Zip文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using JSZip to add selected files into a single zip file. The issue is only the last selected file is being added to the zip (which is also corrupted), see code below:

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

            }

        });
    }

}

任何人都可以看到我的代码存在的问题吗?

Can anyone see the issue with my code?

推荐答案

修复了该问题.问题是我没有将网址正确地放入数组中.

Fixed it. Issue was I wasn't putting the urls into an array correctly.

更正的行:

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

这篇关于无法将多个文件添加到Zip文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆