1.安装各个依赖。npmiaxiosnpmijszipnpmifile-saver2.从“axios”导入axios;从“jszip”导入JSZip;从“file-saver”导入FileSaver;3.方法中的证明方法getFile(url){letthat=this;returnnewPromise((resolve,reject)=>{axios({method:"get",url,responseType:"arraybuffer",onDownloadProgress:function(progressEvent){}}).then(data=>{console.log(数据);解决(数据.数据);}).catch(error=>{reject(error.toString());});});},downfile(){常量数据=[];//需要下载打包的路径可以是本地相对路径,也可以是跨域全路径//this.List是多个下载数据的地址数组this.List.forEach(item=>{data.push(`${item.fileurl`);//item.fileurl添加文件路径到要下载的数据数组});constzip=newJSZip();常量缓存={};const承诺=[];data.forEach(item=>{constpromise=this.getFile(item).then(data=>{//下载文件并将其保存为ArrayBuffer对象constarr_name=item.spl它(”/”);constfile_name=arr_name[arr_name.length-1];//获取文件名zip.file(file_name,data,{binary:true});//一个一个添加文件cache[file_name]=data;});承诺。推(承诺);});Promise.all(promises).then(()=>{zip.generateAsync({type:"blob"}).then(content=>{//生成二进制流FileSaver.saveAs(content,"packageanddownload.zip");//使用file-saver保存文件});});}网上找的方法,如有侵权请联系qq896978695
