This commit is contained in:
X1627315083@163.com
2026-03-18 16:22:16 +08:00
parent 17be81abc9
commit 276115ef65
4 changed files with 221 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
import JSZip from 'jszip'
export const createId = (before: string = 'node') => {
const time = Date.now().toString(36)
const random = Math.random().toString(36).substring(2, 20)
@@ -15,4 +16,47 @@ export const downloadImage = (url: string, name: string) => {
a.download = name || 'image.png'
a.click()
})
}
}
/** 批量下载图片 */
export const downImgListToZip = async (imagesParams) => {
const zip = new JSZip()
const promises = []
// 遍历下载每个图片
imagesParams.forEach((img, index) => {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', img.url, true)
xhr.responseType = "blob";
xhr.onload = () => {
if (xhr.status === 200) {
const fileName = img.name
zip.file(fileName, xhr.response)
resolve('')
} else {
reject(new Error(`下载失败: ${img.url}`))
}
}
xhr.onerror = () => reject(new Error(`网络错误: ${img.url}`))
xhr.send()
})
promises.push(promise)
console.log(promises,zip)
})
// 等待所有图片下载完成
Promise.all(promises)
.then(() => zip.generateAsync({ type: 'blob' }))
.then((content) => {
// 下载zip
console.log(content)
const link = document.createElement('a')
link.href = URL.createObjectURL(content)
link.download = 'DesignFiles'
link.click()
URL.revokeObjectURL(link.href)
})
.catch((error) => console.error('下载失败:', error))
}