vue 前端下载后端传过来的文件流

4 篇文章 0 订阅
该博客介绍了如何在前端通过axios请求,设置responseType为'blob'来接收后端返回的文件流。然后利用浏览器API创建ObjectURL,结合隐藏的a标签实现文件下载。同时,针对IE浏览器的兼容性问题,使用了msSaveBlob方法。这个过程涉及前端与后端交互、文件下载和浏览器兼容性处理。
摘要由CSDN通过智能技术生成

前端下载后端传过来的文件流

首先axios请求中的responseType: ‘blob’

dataDownload(res) {
        const fileName = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]) // 文件名
        const data = res.data // Blob数据对象
        const uA = window.navigator.userAgent
        const isIE =
          /msie\s|trident\/|edge\//i.test(uA) &&
          !!('uniqueID' in document || 'documentMode' in document || 'ActiveXObject' in window || 'MSInputMethodContext' in window)
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        // 兼容IE
        if (isIE) {
          navigator.msSaveBlob(new Blob([data]), fileName)
        } else {
          link.click()
        }
      },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值