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)
})
}
,