更通用的post方法匯出excel檔案
阿新 • • 發佈:2020-08-17
// 更通用的post方法匯出excel檔案 import axios from 'axios' export function excelExport(data, queryUrl, name) { // const data = this.searchForm let url = process.env.VUE_APP_BASE_API_BACK_END_URL + process.env.VUE_APP_BASE_API_BACK_END axios({ method: 'post', url: url + queryUrl, // 請求地址 data: data, //引數 responseType: 'blob', }).then(res => { let newName = name // 處理返回的檔案流 const content = res.data const blob = new Blob([content], { type: 'application/octet-stream;charset=ISO8859-1' }) console.log(res.headers) // const fileName = res.headers['content-disposition'].split('filename=')[1]const fileName = newName if ('download' in document.createElement('a')) { // 非IE下載 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) } else { // IE10+下載 navigator.msSaveBlob(blob, fileName) } }) }