后端返回的二进制文件如何导出
文章目录
这里采用的axios 进行的封装
return axios({
method: 'post',
url: options.url,
headers: {
'content-type': 'application/json',
Accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
},
responseType: 'arraybuffer',
data: options.params
}).then(function (res) {
if (res) {
const url = window.URL.createObjectURL(
**这儿有一个坑 post请求 有写成 res.data get 请求 直接是res**
new Blob([res.data], {
type: 'application/vnd.ms-excel'
})
)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', options.text + '.xls')
document.body.appendChild(link)
link.click()
Message({
message: '导出成功',
type: 'success',
duration: 3000
})
} else {
Message({
message: '导出失败',
type: 'error',
duration: 3000
})
}
})
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论