我的代码是根据jszip 2x运行但现在显示错误"此方法已在JSZip 3.0中删除,请查看升级指南。"。我已经检查了升级指南,但我的代码仍然无效。我需要帮助在angularjs中下载zip文件中的多个图像。提前谢谢。
var zip = new JSZip();
var count = 0;
var zipFilename = "zipFilename.zip";
var urls = [
'http://54.190.15.249/photo/1476967612xyKgSfAlgHEHSt49LfnmvVw3MWTVq4.png',
'http://54.190.15.249/photo/1476967612xyKgSfAlgHEHSt49LfnmvVw3MWTVq4.png',
'http://54.190.15.249/photo/1476967612xyKgSfAlgHEHSt49LfnmvVw3MWTVq4.png'
];
urls.forEach(function(url){
var filename = "filename";
// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err; // or handle the error
}
zip.file(filename, data, {binary:true});
count++;
if (count == urls.length) {
var zipFile = zip.generate({type: "blob"});
saveAs(zipFile, zipFilename);
}
});
});
答案 0 :(得分:0)
错误来自zip.generate
...已在JSZip v3中删除。与upgrade guide一样,您需要使用返回承诺的generateAsync
。
在您的情况下,您可以编写一个函数来包装JSZipUtils.getBinaryContent
并返回结果的承诺(来自here):
/**
* Fetch the content and return the associated promise.
* @param {String} url the url of the content to fetch.
* @return {Promise} the promise containing the data.
*/
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
然后,对于每个网址,将其转换为已下载内容的承诺,并将其添加到您的zip对象(如here):
urls.forEach(function(url){
var filename = url.replace(/.*\//g, "");
zip.file(filename, urlToPromise(url), {binary:true});
}
zip.generateAsync({type:"blob"}).then(function (zipFile) {
saveAs(zipFile, zipFilename);
});
请参阅完整示例here。