下載資料的處理

weixin_33807284發表於2018-12-24

下載後端給的資料生成xls檔案

 // 匯出資料
    pullStoreList() {
      let params = this.form
      params.isJsonData = true
      this.api.post(`url`, params, { responseType: 'blob' }).then(res => {
        this.download(res)
      })
    },
  download(data) {
      if (!data) { return }
      // let text = `${this.schoolName}` + new Date().toLocaleDateString()
      let text = `${this.schoolName}`
      let url = window.URL.createObjectURL(new Blob([data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', `${text}.xls`)
      document.body.appendChild(link)
      link.click()
    },

參考: 深入理解xhr的responseType中blob和arrayBuffer

相關文章