目录
vue前端拿到后端pdf与zip等重新打包为一个新的zip包
code.vue
js
const urlList = [
{
fileUrl:'https://XX.zip',
fileName:'我是文件.zip'
},
{
fileUrl:'https://XXX.pdf',
fileName:'我是pdf.pdf'
}
]
this.downloadZip(urlList)
downloadZip(urlList){
console.log('downloadZip',urlList);
// 创建一个zip
const zip = new JSZip();
// 获取文件 放入zip包中
const fetchAndAddToZip = async (fileUrl, fileName) => {
const response = await fetch(fileUrl);
const fileBlob = await response.blob();
zip.file(fileName, fileBlob, { binary: true });
};
// 生成下载zip包
const generateAndDownloadZip = async () => {
// 遍历把文件丢到zip包中
for (const url of urlList) {
await fetchAndAddToZip(url.fileUrl, url.fileName);
}
// 生成一个zip包
const content = await zip.generateAsync({ type: 'blob' });
// 保存到本地
FileSaver.saveAs(content, 'files.zip');
};
generateAndDownloadZip();
}