前端GET请求下载后端返回数据流文件,并且处理window.open方法跳转白屏方法

 平时常用导出都是用window.open方法     点击跳转连接:使用 window.open 下载

const downError = `地址?&参数=${参数|| ''}`;
const downError = `Url+/xxx/xxx?&orgId=${orgId || ''}`;
 
window.open(downError, "_self");//调用window.open方法导出

而使用window.open时有时候会出现接口异常、或者部分浏览器可能兼容性问题导致会跳转连接白屏的问题

现在用后端接口返回的数据流然后导出(切记添加responseType: 'blob',)

首先配置一下request
export const exportPersonnel = (params) => request({
  url: BaseUrl + '/xxx/xxxx',
  headers: {
    "Project-Id": params.projectId,
  },
  method: 'get',
  responseType: 'blob',		//非常关键!文件流方法
  params,
})
请求:
const params = {
    test: '参数1',
    test2: '参数2',
}

this.props.dispatch({
  type: 'TimelySummary/exportArchiveInfo',
  payload: params,
}).then((ret: any) => {
  if (ret) {
    let blob = new Blob([ret],);
    let objectUrl = (window.URL || window.webkitURL).createObjectURL(blob);
    let downFile = document.createElement("a");
    let fileName = "孜然卷测试报表.xlsx";   //报表名
    downFile.style.display = "none";
    downFile.href = objectUrl;
    downFile.download = fileName;          // 下载后文件名
    document.body.appendChild(downFile);
    downFile.click();
    document.body.removeChild(downFile);   // 下载完成移除元素
    window.URL.revokeObjectURL(objectUrl); // 只要映射存在,Blob就不能进行垃圾回收,因此一旦不再需要引用,就必须小心撤销URL,释放掉blob对象。
  }
})

另外post请求可查看文章  点击跳转连接:POST请求导出后端返回数据流

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 07:44:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 07:44:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 07:44:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 07:44:02       18 阅读

热门阅读

  1. Unity3D安装图文详细步骤

    2024-05-13 07:44:02       10 阅读
  2. 第十三届蓝桥杯国赛

    2024-05-13 07:44:02       10 阅读
  3. OpenCV 光流法总结

    2024-05-13 07:44:02       7 阅读
  4. boto3库调用AWS大模型的封装类

    2024-05-13 07:44:02       11 阅读
  5. 排序算法 下

    2024-05-13 07:44:02       13 阅读
  6. Vue简介

    2024-05-13 07:44:02       12 阅读
  7. Yarn使用

    2024-05-13 07:44:02       9 阅读
  8. TensorFlow基于anaconda3快速构建

    2024-05-13 07:44:02       9 阅读
  9. redis面试

    2024-05-13 07:44:02       9 阅读
  10. MySQL数据库基础功能

    2024-05-13 07:44:02       8 阅读