前端下载接口文件流

174 阅读1分钟

网络请求(responseType: 'blob' 在封装的axios中使用不生效,具体还没研究,所以直接使用axios基础的)

axios({
        method: 'GET',
        url: process.env.BASE_API+'/importExport/excel/export',
        params: this.listQuery,
        responseType: 'blob'
    }).then(res=>{
    })

返回then()处理代码

          let blob = new Blob([res.data], {
            type: "application/vnd.ms-excel;charset=utf-8"
          });
          let url = window.URL.createObjectURL(blob);
          console.log(url)
          let aLink = document.createElement("a");
          aLink.style.display = "none";
          aLink.href = url;
          aLink.setAttribute("download", "导出记录.xls"); // 下载的文件
          document.body.appendChild(aLink);
          aLink.click();
          document.body.removeChild(aLink);
          window.URL.revokeObjectURL(url)