前端使用JSZip和file-saver将url链接文件打包压缩导出

108 阅读1分钟

前端使用JSZip将url链接文件打包压缩导出

<script>
import saveAs from "file-saver";
import JSZip from "jszip";
    const downloadAll = async()=>{
     const urlList=["https://static.erp.zhongguo.cn/dev/task/2024-07/01/scxw5/学生信息666.xlsx","https://static.erp.zhongguo.cn/dev/task/2024-07/01/4g8yy/范德萨范德萨发佛挡杀.pptx"]
     const zip = new JSZip();
     // 这里不能使用map循环,要换成for循环
     for(var i = 0;i<urlList.length;i++){
       const res = await fetch(urlList[i]);
       const blob = await res.blob();
       const fileName = urlList[i].split('/').pop();
       zip.file(fileName,blob)
     }
     //检查zip包中包含的文件数量
     //const fileCount = Object.keys(zip.files).length;
     //console.log('fileCount',fileCount)
     //生成zip文件
     zip.generateAsync({type:'blob'}).then(blob=>{
       saveAs(blob,'压缩包.zip')
     })
    }
</script>