mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-manage.git
synced 2025-12-16 14:26:45 +08:00
合并zcwy-dev
This commit is contained in:
33
src/hooks/useDownload.js
Normal file
33
src/hooks/useDownload.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import http from "@/api/configSys";
|
||||
/**
|
||||
* @param {String} url [请求的url地址]
|
||||
* @param {Object} params [参数]
|
||||
* @param {String} fileName [导出文件名称] 默认值 导出文件
|
||||
* @param {String} fileType [导出文件类型] 默认值 xls
|
||||
* @param {string} mimeType [导出文件类型]
|
||||
*/
|
||||
const useDownload = (url, params = {}, fileName = '导出文件', fileType = 'xls', mimeType = 'application/vnd.ms-excel;charset=UTF-8') => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.post(url, params, { responseType: 'blob' })
|
||||
.then(res => {
|
||||
resolve(res.data);
|
||||
if (!res.data) {
|
||||
return
|
||||
}
|
||||
const link = document.createElement('a');// 创建a标签
|
||||
let blob = new Blob([res.data], { type: mimeType }); // 设置文件类型
|
||||
link.style.display = "none";
|
||||
link.href = URL.createObjectURL(blob); // 创建URL
|
||||
link.setAttribute("download", `${fileName}.${fileType}`);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
URL.revokeObjectURL(link.href);
|
||||
document.body.removeChild(link);
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err.data);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
export default useDownload
|
||||
Reference in New Issue
Block a user