axios封装导出功能
This commit is contained in:
@@ -585,4 +585,37 @@ export default {
|
||||
})
|
||||
},
|
||||
|
||||
_downloadFile (url, params, method = 'get', fileName = '导出文件') {
|
||||
_axios({
|
||||
method,
|
||||
url,
|
||||
data: method !== 'get' ? params : {},
|
||||
params: method === 'get' ? params : {},
|
||||
responseType: 'blob'
|
||||
}).then(
|
||||
res => {
|
||||
const data = res.data
|
||||
// 兼容ie
|
||||
if (window.navigator.msSaveOrOpenBlob) {
|
||||
try {
|
||||
const blobObject = new Blob([data])
|
||||
window.navigator.msSaveOrOpenBlob(blobObject, fileName)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
return
|
||||
}
|
||||
// a标签实现下载
|
||||
// type为导出文件格式 例application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 导出为xlsx格式
|
||||
let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }))
|
||||
let a = document.createElement('a')
|
||||
a.style.display = 'none'
|
||||
a.href = url
|
||||
a.setAttribute('download', fileName)
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url); // 释放对象URL
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user