mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 10:29:19 +08:00
refactor(ecom-dmb): 导出服务重构,ExportServiceImpl拆分为独立服务类,抽取ExportFilenameUtils统一文件名构建
This commit is contained in:
@@ -14,6 +14,26 @@ function formatToken(token: null | string) {
|
||||
return token ? `Bearer ${token}` : null;
|
||||
}
|
||||
|
||||
/** 从 Content-Disposition 解析文件名(与后端 attachment; filename="..." 对齐) */
|
||||
function parseFilenameFromContentDisposition(
|
||||
contentDisposition: string | null,
|
||||
): string | null {
|
||||
if (!contentDisposition) return null;
|
||||
const utf8Match = /filename\*=UTF-8''([^;\s]+)/i.exec(contentDisposition);
|
||||
if (utf8Match?.[1]) {
|
||||
try {
|
||||
return decodeURIComponent(utf8Match[1].replace(/"/g, ''));
|
||||
} catch {
|
||||
return utf8Match[1].replace(/"/g, '');
|
||||
}
|
||||
}
|
||||
const quoted = /filename="([^"]+)"/i.exec(contentDisposition);
|
||||
if (quoted?.[1]) return quoted[1];
|
||||
const plain = /filename=([^;\s]+)/i.exec(contentDisposition);
|
||||
if (plain?.[1]) return plain[1].replace(/^"|"$/g, '');
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function exportByPost(
|
||||
url: string,
|
||||
params: Record<string, unknown>,
|
||||
@@ -55,11 +75,16 @@ export async function exportByPost(
|
||||
return;
|
||||
}
|
||||
|
||||
const headerName =
|
||||
parseFilenameFromContentDisposition(
|
||||
response.headers.get('Content-Disposition'),
|
||||
) || filename;
|
||||
|
||||
// 触发浏览器下载
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = downloadUrl;
|
||||
link.download = filename;
|
||||
link.download = headerName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
|
||||
Reference in New Issue
Block a user