1,判断是否ie打开的方法
isIE(){
let ua = navigator.userAgent.toLocalLowerCasse()
return ua.match(/mise/)!=null || ua.match(/trident/) != null
}
使用navigator.userAgent.toLowerCase()判断登陆端是pc还是手机_H小志-CSDN博客blog.csdn.net![d4284d42d19a97f30cbb40798f98e02b.png](https://i-blog.csdnimg.cn/blog_migrate/f78da2e3584d1bc4752ccf0a9d3150fb.png)
2.使用axios下载文件流
axios(
{
url:baseurl+"接口文件路径",
method:"get",
headers:{
authorization:getToken()
},
responeseType:"blob" //表示服务器响应的数据类型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
}
).then(res=>{
//链接地址:responseType设置了blob,实际返回了JSON格式的错误信息的处理方式
当返回错误信息时,后台返回的结果为json格式,当结果为正确时,返回文件流
if(res.data.size<1000){此时返回的是提示错误信息的json对象
var reader = new FileReader();
reader.readAsText(res.data)
reader.onload = function(){
let data = JSON.parse(reader.result)
if(){
//错误信息处理
}
}
}else{
//文件流处理
if(this.ISIE()){
let blob = new Blob([res.data],res.headers['content-type'])
window.navigator.msSaveOrOpenBlob(blob,_fileName)
}else{
let a = document.createElement('a');
a.style.display="none"
a.href = window.URL.createObjectURL(res.data)
a.setAttribute('download',_fileName)
document.body.appendchild(a)
a.click()
}
}
})