【VUE】Request background, export form

The file export table method here is:
process and download according to the value returned by the backend. (Pay attention to the data structure returned by the backend, because it will affect the normal conversion of the file stream, resulting in garbled characters in the downloaded table)

Parameter description:
token: account login token (check whether the background needs to be sent back, if not, do not need to pass)
res.data: returned by the background, you can directly convert the downloaded object data

// 导出表格
    downloadExcel() {
    
    
      var that = this
      this.axios.get('/api/exportExcel', {
    
    
        responseType: 'blob',
        headers: {
    
     token: that.token, 'Content-Type': 'application/x-download' }
      }).then((res) => {
    
    
        if (res.status === 200) {
    
    
          var a = document.createElement('a')
          var blob = new Blob([res.data], {
    
     type: 'application/vnd.ms-excel' })
          var href = window.URL.createObjectURL(blob) // 创建下载的链接
          a.href = href
          a.download = '表格.xlsx' // 下载后文件名
          document.body.appendChild(a)
          a.click() // 点击下载
          document.body.removeChild(a) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
        }
      }).catch((err) => {
    
    
        console.log(err)
      })
    }

Guess you like

Origin blog.csdn.net/LuviaWu/article/details/118088503