后台直接返回文件的二进制内容,然后前端转化一下再下载
axios({
method: 'post',
url: '/export',
responseType: 'arraybuffer', // 注意没有设置responseType的话下载的是乱码
})
.then(res => {
// 假设 data 是返回来的二进制数据
const data = res.data
const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', 'excel.xlsx')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})