我正在使用 JSZip 生成包含多个文件的 zip。数据以 JSON 格式来自服务器。我在 for 循环中只得到 2 个文件,而不是多个文件。JSON 数组的大小超过 2,所以我希望多个文件不仅仅是 2。
var zip = new JSZip();
var root = data.list.dataList;
var length = root.length;
var i;
for(i = 0; i < length; i++){
var aFileName = "a_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file
var aContent = root[i].content; //content of file
var bFileName = "b_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file
var bContent = root[i].content; //content of file
//put files onto a folder
zip.folder("Folder Name").file(aFileName, aContent).file(bFileName, bContent);
}
//generate the zip with all files
var content = zip.generate();
location.href="data:application/zip;base64," + content;
有人可以提出替代方案,因为此代码仅采用最后一次迭代的内容。这就是为什么我只能获取 2 个文件而不是多个文件的原因。