30 lines
849 B
JavaScript
30 lines
849 B
JavaScript
import JSZip from 'jszip'
|
|
import { saveAs } from 'file-saver'
|
|
|
|
export async function downloadFilesAsZip(fileObject) {
|
|
const zip = new JSZip()
|
|
for (const item of fileObject.files) {
|
|
try {
|
|
const response = await fetch(item.url)
|
|
if (!response.ok) {
|
|
throw new Error(`下载失败 ${item.url}: ${response.status}`)
|
|
}
|
|
const blob = await response.blob()
|
|
// 提取文件名,假设文件路径中最后一部分为文件名
|
|
const fileName = item.name
|
|
zip.file(fileName, blob)
|
|
} catch (error) {
|
|
console.error(`下载失败 ${item.url}:`, error)
|
|
}
|
|
}
|
|
|
|
// 生成压缩文件
|
|
try {
|
|
const content = await zip.generateAsync({ type: 'blob' });
|
|
// 保存压缩文件
|
|
saveAs(content, fileObject.zipName);
|
|
} catch (error) {
|
|
console.error('生成ZIP错误', error);
|
|
}
|
|
}
|