responseType: “arraybuffer” 是关键
export function filePost(url, params, config) {
let userAuth = JSON.parse(sessionStorage.getItem("userAuth"));
return new Promise((resolve, reject) => {
axios.create({
baseURL: baseUrl,
timeout: 15000,
headers: {
"Content-Type": "application/json-patch+json",
"Authorization": userAuth.token_type + " " + userAuth.access_token,
},
responseType: "arraybuffer" //必须设置,不然导出的文件无法打开
})
.post(url, params, config)
.then(res => {
resolve(res);
})
.catch(err => {
reject(err);
});
});
}
导出接口:
const exportExl = () => {
store
.exportExl({
shop: 'shopNumber,shopName,road,shopAddress',
shopNumber: 'S000075,S000002,S000077',
})
.then((res) => {
const blob = new Blob([res], {
type: 'application/vnd.ms-excel;charset=utf-8',
})
const fileName = '部队信息.xlsx'
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
})
}