Files
td_official/src/utils/useFileDownload.js
2025-08-20 19:21:52 +08:00

64 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ElMessage } from 'element-plus';
/**
* 下载文件下载工具函数
* @param {string} fileUrl - 文件地址
* @param {string} [fileName] - 可选,指定文件名
* @param {Object} [headers] - 可选请求头如携带token
*/
export const downloadFile = async (fileUrl, fileName, headers = {}) => {
try {
// 发起请求获取文件数据
const response = await fetch(fileUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/octet-stream',
...headers
},
credentials: 'include' // 允许携带cookie
});
if (!response.ok) {
throw new Error(`下载失败: ${response.statusText}`);
}
// 将响应转换为blob对象
const blob = await response.blob();
// 创建临时URL
const url = URL.createObjectURL(blob);
// 处理文件名
let downloadName = fileName;
if (!downloadName) {
// 从响应头获取文件名
const contentDisposition = response.headers.get('content-disposition');
if (contentDisposition) {
const match = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
if (match && match[1]) {
downloadName = decodeURIComponent(match[1].replace(/['"]/g, ''));
}
} else {
// 从URL提取文件名
downloadName = fileUrl.split('/').pop()?.split('?')[0] || 'download_file';
}
}
// 创建a标签并触发下载
const link = document.createElement('a');
link.href = url;
link.download = downloadName;
link.style.display = 'none'; // 隐藏a标签
document.body.appendChild(link);
link.click();
// 清理资源
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(url); // 释放临时URL
}, 100);
} catch (error) {
ElMessage.error(`文件下载失败: ${error.message}`);
}
};