64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
|
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}`);
|
|||
|
}
|
|||
|
};
|