浏览器端JSZip将文件压缩打包下载

 const zip = new JSZip()
    result.list.map((val, index) => {
      zip.file(index + '.jpg', val, { base64: true })
    })
    zip.generateAsync({ type: 'blob' }).then((blob: Blob) => {
      saveAsByBlob(result.delivery_code + '.zip', blob)
    })
export function saveAsByBlob(fileName: string, blob: Blob) {
  const link = getLinkElem(fileName)
  const url = URL.createObjectURL(blob)
  link.href = url
  setTimeout(() => {
    link.click()
  }, 0)
  setTimeout(() => {
    URL.revokeObjectURL(url)
  }, 3e5)
}
function getLinkElem(download: string) {
  const link = document.createElement('a')
  const name = download || 'download'
  link.rel = 'noopener'
  link.setAttribute('download', name)
  return link
}
Last Updated:
Contributors: 刘荣杰