zoukankan      html  css  js  c++  java
  • vue前端导出zip包

    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     }
  • 相关阅读:
    js键盘事件以及键盘事件拦截
    JavaScript 延迟加载
    二叉树深度优先 求二叉树最大深度
    css 小知识点:inline/inline-block/line-height
    es6 set
    CSS 水平垂直居中
    js 位运算符
    js 函数重载
    js之单例模式
    js 面向对象 ES5 AND ES6
  • 原文地址:https://www.cnblogs.com/yangguoe/p/9405173.html
Copyright © 2011-2022 走看看