js如何下载后端返回的二进制文件

async downloadFile() {
const cname = this.loginUser.username
const params = await this.getParams()//自己写的获取参数方法
axios({
url: ${baseURL}/download,
method: 'post',
data: params,
responseType: 'blob',
headers: {
cid: this.customerCode
}
}).then(res => {
const fileName = wifi预警_${cname}.csv
var blob = res.data
if ('msSaveOrOpenBlob' in navigator) {
window.navigator.msSaveOrOpenBlob(blob, fileName) //IE导出
} else {
let url = window.URL.createObjectURL(new Blob([blob]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
link.click()
}
})
},

作者:小小外星人ღ
链接:https://juejin.cn/post/6844904086270279688
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(js如何下载后端返回的二进制文件)