vue前端拿到后端pdf与zip等重新打包为一个新的zip包
code.vue
const urlList = [
{
fileUrl:'https://XX.zip',
fileName:'我是文件.zip'
},
{
fileUrl:'https://XXX.pdf',
fileName:'我是pdf.pdf'
}
]
this.downloadZip(urlList)
downloadZip(urlList){
console.log('downloadZip',urlList);
const zip = new JSZip();
const fetchAndAddToZip = async (fileUrl, fileName) => {
const response = await fetch(fileUrl);
const fileBlob = await response.blob();
zip.file(fileName, fileBlob, { binary: true });
};
const generateAndDownloadZip = async () => {
for (const url of urlList) {
await fetchAndAddToZip(url.fileUrl, url.fileName);
}
const content = await zip.generateAsync({ type: 'blob' });
FileSaver.saveAs(content, 'files.zip');
};
generateAndDownloadZip();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36