接口要求: post方法、入參為json格式、出參文件流
axios:設置返回數據格式為 blob 或者 arraybuffer ( 注意 )
axios({ // 用axios發送post請求
method: 'post',
url: ' /serviceTime/exportData', // 請求地址
data: form, // 參數
responseType: 'blob', // 表明返回服務器返回的數據類型
headers: {
'Content-Type': 'application/json'
}
}).then(res => { // 處理返回的文件流
const blob = new Blob([res.data]);//new Blob([res])中不加data就會返回下圖中[objece objece]內容(少取一層)
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);
})