利用 js 合并多个图片 下载 zip 发表于 2021-05-31 | 更新于 2023-01-11 利用 js 合并多个图片 下载 zip注意:图片地址要允许跨域或者在同域名 12345678910111213141516171819202122232425262728293031323334// downloadImagesZip.jsimport axios from 'axios'import JSZip from 'jszip'// 获取图片arraybufferasync function getFileData(url) { const { data } = await axios({ url, responseType: 'arraybuffer' }) return data}// 利用a标签下载async function downloadBlob(blob, name = 'images.zip') { const href = URL.createObjectURL(blob) const a = document.createElement('a') a.download = name a.style.display = 'none' a.href = href document.body.appendChild(a) a.click() document.body.removeChild(a)}// 批量下载export default async function downloadImagesZip(data = []) { const zip = new JSZip() await Promise.all( data.map(async (url) => { const file = await getFileData(url) return zip.file(url.split('/').pop(), file, { binary: true }) }) ) const blob = await zip.generateAsync({ type: 'blob' }) downloadBlob(blob, 'images.zip')} 12345678// test.jsimport downloadImagesZip from './downloadImagesZip'downloadImagesZip([ 'https://img1.halobear.com/upload_page/Fl_tXi-5tqSvPMs1T46YK7zGoFBF.jpg', 'https://img1.halobear.com/upload_page/FqAd3-L1NstwZzTadovSXqfJc-j8.jpg',]) 参考 jszip