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

JavaScript021

js如何下载后端返回的二进制文件,第1张

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()

}

})

},

最近在做blob流导出相关功能,其中需要导出excel、csv、word、zip压缩文件之类的,在导出excel和word中需要知道对应的content-type属性,关于blob格式如下: