vue中下载--后端返回的文档流

 实现后端返回的文档流,点击下载

<div class="prom-add" @click="downLoad"><i class="el-icon-download"></i>下载</div>
 import request from '@/router/axios'
//下载
    async downLoad(){
      let params = {
        start: this.dateVal[0],
        end: this.dateVal[1],
        platform: this.value1,
      }
      let res =  await exportInviteSourceData(params) //后端返回的文档流,如上图
      
      this.downBlobFile('/admin/frontedUsers/export', res.data, '邀请/注册.xlsx')
    },
  
downBlobFile(url, query, fileName) {
  let config = {
    url: url,
    method: 'get',
    responseType: 'blob',
    params: query
  }
  if (!url.startsWith('/')) {
    config.withCredentials = false
  }
  return request(config).then((response) => {
    // 处理返回的文件流
    const blob = response.data
    if (blob && blob.size === 0) {
      this.$notify.error('内容为空,无法下载')
      return
    }
    const link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    window.setTimeout(function () {
      window.URL.revokeObjectURL(blob)
      document.body.removeChild(link)
    }, 0)
  })
}

 

posted @ 2023-12-21 10:07  IT小姐姐  阅读(35)  评论(0编辑  收藏  举报