接口请求下载文件

使用< a>标签
<a href="../../static/xxx.xlsx" download="xxx.xlsx">下载</a>

直接点击可以下载,需要注意的是download属性,当不加download属性时,如果文件格式为txt、pdf、jpg等浏览器支持直接打开的文件格式,那么不会下载,而是浏览器直接打开;添加download属性之后,就会下载,并且下载文件默认命名为你download属性的值

使用window.open()
window.open("../../static/xxx.xlsx")
 
window.open("https://cee/edit/xxx.xlsx")
返回值得处理,其实也是< a>标签

首先是请求接口会返回res,需要注意请求一定要加 responseType: "blob"哦!然后再调用下面封装的方法即可

axios.get({
    url: 'xxxxxx',
    method: 'get',
    data:{},
    responseType: 'blob'
}).then(res => {});
  ExportCSV(res) {
    const blob = new Blob([res.data], {
      type: "application/vnd.ms-excel"
    })
    const filename = decodeURI((res.headers['content-disposition'] && res.headers['content-disposition'].split(';')[1].split('=')[1])) || 'file.xls'
    // 创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob, filename);
    } else {
      const elink = document.createElement('a')
      elink.download = filename
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

守望黑玫瑰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值