1. npm install jszip /npm install script-loader / npm install file-saver
2.功能代码
1 require('script-loader!file-saver'); 2 import JSZip from 'jszip' 3 4 export function export_txt_to_zip(th, jsonData, txtName, zipName) { 5 const zip = new JSZip() 6 const txt_name = txtName || 'file' 7 const zip_name = zipName || 'file' 8 const data = jsonData 9 let txtData = `${th} ` 10 data.forEach((row) => { 11 let tempStr = '' 12 tempStr = row.toString() 13 txtData += `${tempStr} ` 14 }) 15 zip.file(`${txt_name}.txt`, txtData) 16 zip.generateAsync({ 17 type: "blob" 18 }).then((blob) => { 19 saveAs(blob, `${zip_name}.zip`) 20 }, (err) => { 21 alert('导出失败') 22 }) 23 }
3.调用代码:
1 handleDownload() { 2 this.downloadLoading = true 3 import('@/vendor/Export2Zip').then(zip => { 4 const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date'] 5 const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time'] 6 const list = this.list 7 const data = this.formatJson(filterVal, list) 8 zip.export_txt_to_zip(tHeader, data, this.filename, this.filename) 9 this.downloadLoading = false 10 }) 11 }, 12 formatJson(filterVal, jsonData) { 13 return jsonData.map(v => filterVal.map(j => v[j])) 14 }