我正在使用jsZip将多个文件保存到zip文件中并下载。我不知道如何从urls数组中输出该迭代的数组名称。
urls数组是应该放在var filename = "";
内的文件名,只是找不到一种方法来打印出每个数组名。
var zip = new JSZip();
var count = 0;
var urls = [
"FirstFile.pdf",
"SecondFile.pdf",
];
urls.forEach(function(url)
{
//if iteration #1, then echo firstFile.pdf below,
//if iteration #2 echo SecondFile.pdf below so it saves the files inside the zip.
var filename = "output urls array here as name";
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err; // or handle the error
}
zip.file('./temp/' + filename, data, {binary:true});
count++;
if (count == urls.length)
{
zip.generateAsync({type:'blob'}).then(function(content)
{
$( ".download" ).click(function() {
saveAs(content, 'FileZip.zip');
});
});
}
});
});
最佳答案
文件名存储在url
参数中,该参数传递给与foreach
一起使用的匿名函数。
因此,要在任何迭代中访问文件名,只需使用url
var filename = url;
实际上,您实际上并不需要第二个名为filename的变量,只需将后面的行修改为
zip.file('./temp/' + url, data, {binary:true});
关于javascript - 每次迭代都在forEach中打印出数组值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43245506/