应在请求中设置返回类型:responseType: "blob"
axios.get(
'/api/url',
{
params: {
id: '11',
},
},
{
headers: {
'responseType ': 'blob',
},
}
)
.then((res) => {
if (res.status == 200) {
// 下载文档流
const blob = res.data
let link = document.createElement('a')
link.href = URL.createObjectURL(
new Blob([blob], { type: 'application/vnd.ms-excel' })
)
link.download = this.form.fileName
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
} else {
self.$baseMessage(res.data, 'error')
}
})
.catch((res) => {
self.$baseMessage(res.data, 'error')
})
},