Delete path/to/frontend/file
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
import { request } from '@/utils/request'; // 假设这是你的请求函数
|
||||
import { ElMessage } from 'element-plus'; // Element Plus 的消息组件
|
||||
import { t } from '@/locales'; // 国际化函数
|
||||
import { saveFileByWebApi, saveFileByElementA } from '@/utils/file'; // 文件保存工具函数
|
||||
|
||||
const downloadFile = (id: number) => {
|
||||
request({
|
||||
url: '/admin/file/download',
|
||||
method: 'get',
|
||||
params: {
|
||||
id,
|
||||
},
|
||||
responseType: 'blob'
|
||||
}).then((response: any) => {
|
||||
const contentDisposition = response.headers?.['content-disposition'] || '';
|
||||
let filename = 'file';
|
||||
|
||||
try {
|
||||
// 先尝试获取 filename* 参数
|
||||
const filenameStarMatch = contentDisposition.match(/filename\*=UTF-8''([^;]*)/);
|
||||
if (filenameStarMatch) {
|
||||
filename = decodeURIComponent(filenameStarMatch[1]);
|
||||
} else {
|
||||
// 如果没有 filename*,则尝试获取普通 filename
|
||||
const filenameMatch = contentDisposition.match(/filename="([^"]*)"/) || contentDisposition.match(/filename=([^;]*)/);
|
||||
if (filenameMatch) {
|
||||
filename = filenameMatch[1];
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('解析文件名失败:', error);
|
||||
// 如果解析失败,使用默认文件名
|
||||
filename = `download_${id}`;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
if (window.showSaveFilePicker) {
|
||||
saveFileByWebApi(response.data, filename).catch(() => {
|
||||
saveFileByElementA(response.data, filename).catch(() => {
|
||||
ElMessage.error(t('admin.fileView.download_fail'));
|
||||
});
|
||||
});
|
||||
} else {
|
||||
saveFileByElementA(response.data, filename).catch(() => {
|
||||
ElMessage.error(t('admin.fileView.download_fail'));
|
||||
});
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('下载文件失败:', error);
|
||||
ElMessage.error(t('admin.fileView.download_fail'));
|
||||
});
|
||||
};
|
||||
|
||||
export default downloadFile;
|
||||
Reference in New Issue
Block a user