vue中使用axios下载文件,兼容IE11

本文介绍如何设置axios返回Blob数据,并通过a标签或IE浏览器的navigator.msSaveBlob方法实现文件下载功能,适用于不同浏览器环境。

一、设置axios返回值为blob

二、使用a标签的down属性下载,如果是IE浏览器,可以使用navigator.msSaveBlob进行下载

// data的数据类型是blob
downloadFiles (data) {
  if (!data) {
    return
  }
  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', '消费码.zip')

  document.body.appendChild(link)
  // 兼容IE
  if (isIE) {
    navigator.msSaveBlob(new Blob([data]), '消费码.zip')
  } else {
    link.click()
  }
}

 

转载于:https://www.cnblogs.com/llcdxh/p/10109624.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值