实现前端导出功能,一顿乱写后发现导出乱码
测试直接调后台接口,没问题;已设置responseType:'blob',相同的代码,在两个前端门户中,一个正常,一个导出乱码(最怕这种问题)
于是乎开始了漫长的找坑之旅
先看代码:
axios({
method: 'post',
url: `/downloadUrl`, // 请求地址
data: this.searchForm, // 请求数据
responseType: 'blob' // 表明返回服务器返回的数据类型 这里注意要加上responseType
}).then((res) => { // 处理返回的文件流
const blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=UTF-8'})
const fileName = 'down.xlsx'
const alink = document.createElement('a')
alink.download = fileName
alink.style.display = 'none'
alink.href = URL.createObjectURL(blob) // 这里是将文件流转化为一个文件地址
document.body.appendChild(alink)
alin