导出功能
前提:后台返回二进制文件数据、axios、Blob、ES6
浏览器兼容:FF/IE10/IE11/CHROME
代码:
import axios from 'axios'
let axio = axios.create({
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
responseType:'blob' //返回数据的格式为blob
})
axio({
url: 后端api,
method: 'get/post',
params/data: 参数
}).then(res => {
let blob = new Blob([res.data],{type:'要导出文件类型MIME类型'})
if('msSaveOrOpenBlob' in navigator){
window.navigator.msSaveOrOpen(blob,'***')
}else{
let downElement = document.createElement('a')
let url = window.URL.createObjectURL(blob)
downElement.href = url
downElement.download = '***文件名称***'
document.body.appendChild(downElement)
downElement.click()
document.body.removeChild(downElement)
window.URL.revokeObjectURL(url)
}
感慨一下:MDN/MSDN是个好网站~