前端 文件流下载blob

1.axios请求携带 responseType: 'blob'

export function getCreateReport(id) {
  return request({
    url: `${apiPrefix}/bus/proposal/evaluation/${id}`,
    method: 'Get',
    responseType: 'blob'
  })
}

2.编写下载方法

复制代码
// blob文件下载
export function downBlobFile(fileName, fileBlob) {
  const link = document.createElement('a')
  try {
// let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'}); //如果后台返回的不是blob对象类型,先定义成blob对象格式,type格式根据返回文件不同修改 const blob = fileBlob const _fileName = fileName // 拆解获取文件名,如果后端没返回文件格式,则需要自己拼接文件后缀 link.style.display = 'none' // 兼容浏览器 const url = window.URL || window.webkitURL || window.moxURL link.href = url.createObjectURL(blob) // 如果没设置axios请求reponseType为‘blob’的话,则参数应为new Blob(文件流),否则报错 link.download = _fileName // 下载的文件名称 link.click() window.URL.revokeObjectURL(url) // #URL.revokeObjectURL()方法会释放一个通过URL.createObjectURL()创建的对象URL. 当你要已经用过了这个对象URL,然后要让浏览器知道这个URL已经不再需要指向对应的文件的时候,就需要调用这个方法. } catch (error) { console.error(error.message) } }
复制代码

3.调用函数

      const result = await getCreateReport(id)
      downBlobFile(`商标申请评估报告`, result.data)

posted on   ChoZ  阅读(228)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示