mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-manage.git
synced 2025-12-16 22:36:45 +08:00
33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
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 |