后台返回乱文文件流前端下载excel表格并从响应头取出文件名
exportData() {
this.loading = true
let param = {
pageStart: 1,
pageLen: 100000
}
let url = '/apis/event/exportEventInfoList?'
axios({
method: 'get',
url: url + qs.stringify(param),
responseType: 'blob',
headers: {
'Content-Type': 'application/json'
}
// getResponse: true
})
.then((res) => {
if (res.status === 200) {
const link = document.createElement('a')
let blob = new Blob([res.data], { type: 'application/x-excel' })
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
// 从响应头获取文件名
const filename = res.headers?.['content-disposition'].split('attachment;filename=')[1]
link.download = decodeURI(filename)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
this.$notify({
title: '成功',
message: '导出成功',
type: 'success',
duration: 1000
})
this.loading = false
} else {
this.loading = false
this.$message({
type: 'error',
message: '导出失败'
})
}
})
.catch(() => {
this.loading = false
this.$notify.error({
title: '错误',
message: '导出失败',
duration: 1000
})
})
}```