// 将文件夹里的文件作为数组传进来
filesToRar(files) {
this.fileCount = files.length
this.zippedFileCount = 0
let zip = new JSZip();
this.zipDir(zip, files, zip);
},
// 循环下载数组中的每个文件
zipDir(zip, files, packageZip) {
for (let i = 0; i < files.length; i++) {
let item = files[i];
this.zipFile(zip, item, packageZip);
}
},
async zipFile(zip, file, packageZip) {
let res = await axios({
method: 'get',
url: file.downloadUrl,
responseType: 'blob',
timeout: 4 * 60 * 60 * 1000,
onDownloadProgress:(event)=> {
}
});
if(res && res.data) {
zip.file(file.name, res.data);
this.zippedFileCount++
}
// 所有文件都下载之后,进行打包下载
if (this.fileCount === this.zippedFileCount) {
// 把打包内容异步转成blob二进制格式
packageZip.generateAsync({ type: "blob" }, function updateCallback(metadata){
console.log(metadata.percent.toFixed(2));
}).then((content) => {
saveAs(content, this.floderdown.customName);
}).catch((res)=>{
// this.downloadMsg= "压缩失败"
});
}
}