通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件
axios({
method: 'post',
url: url, // 请求地址
data: options, // 参数
responseType: 'blob' // **表明返回服务器返回的数据类型**
}).then(
response => {
let blob = new Blob([response.data], {
type: 'application/vnd.ms-excel'
})
let fileName = Date.parse(new Date()) + '.xlsx'
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie1
navigator.msSaveBlob(blob, fileName)
} else {
var link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
//释放内存
window.URL.revokeObjectURL(link.href)
}
}
)
很重要,ajax的类型一定要写-------responseType: ‘blob’, //返回数据的格式,可选值为arraybuffer,blob,document,json,text,stream,默认值为json
设置文件的下载类型
var blob = new Blob([res.data], {type: ‘text/csv’}); //这里表示xlsx类型
Blod.type的地址