我正在使用jsZip將多個文件保存到zip並下載它。我無法弄清楚如何從urls數組中輸出該迭代的數組名稱。在每次迭代中輸出forEach中的數組值
這個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');
});
});
}
});
});
我不明白爲什麼會發生這種情況。你是否嘗試將連接移出到一個單獨的行並顯示結果? 'var filename ='./temp/'+ url; console.log(文件名);' – domwrap