vue前端导出功能在ie11中的兼容问题解决

在用vue做前端开发时,需要对列表加一个导出功能,如下写法,没有else里面的,在ie之外的浏览器都可以正常导出,可是在ie里却没有反应,后来加上else 里面这句即可以了。
在这里插入图片描述
代码是这样的:
expor(this.exportDatas).then(res => {
const blob = new Blob([res.data])
const fileName = ‘某表.xls’
if (‘download’ in document.createElement(‘a’)) {
const a = document.createElement(‘a’)
a.href = URL.createObjectURL(blob)
a.download = fileName
a.style.display = ‘none’
document.body.appendChild(a)
a.click()
URL.revokeObjectURL(a.href)
document.body.removeChild(a)
} else {
navigator.msSaveBlob(blob, fileName)
}
}).catch((error) => {
this.$message.error(error)
})
工作中遇到的问题,备份一个。
更新:
如上方法在win10 的edge浏览器中不兼容,所以代码改成如下方法就可以顺利兼容chrome , firefox, IE, edge 了
在这里插入图片描述
代码:
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
}else{
const a = document.createElement(‘a’)
a.href = URL.createObjectURL(blob)
a.download = fileName
a.style.display = ‘none’
document.body.appendChild(a)
a.click()
URL.revokeObjectURL(a.href)
document.body.removeChild(a)
}
~~各种浏览器的兼容真烧心:)

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值