refactor(annotation): 简化注释数据导出下载逻辑

- 移除前端手动创建 a 标签下载文件的方式
- 将文件名参数传递给后端 API 函数
- 利用 download 函数内置的下载处理机制
- 简化 ExportAnnotationDialog 组件中的导出流程
- 更新 annotation.api.ts 中的 downloadAnnotationsUsingGet 函数签名
- 直接通过 API 调用完成文件下载和命名
This commit is contained in:
2026-01-30 17:33:14 +08:00
parent 6dfed934a5
commit a4cdaecf8a
2 changed files with 11 additions and 18 deletions

View File

@@ -106,13 +106,6 @@ export default function ExportAnnotationDialog({
const values = await form.validateFields(); const values = await form.validateFields();
setExporting(true); setExporting(true);
const blob = await downloadAnnotationsUsingGet(
projectId,
values.format,
values.onlyAnnotated,
values.includeData
);
// 获取文件名 // 获取文件名
const formatExt: Record<ExportFormat, string> = { const formatExt: Record<ExportFormat, string> = {
json: "json", json: "json",
@@ -124,15 +117,14 @@ export default function ExportAnnotationDialog({
const ext = formatExt[values.format as ExportFormat] || "json"; const ext = formatExt[values.format as ExportFormat] || "json";
const filename = `${projectName}_annotations.${ext}`; const filename = `${projectName}_annotations.${ext}`;
// 下载文件 // 下载文件(download函数内部已处理下载逻辑)
const url = window.URL.createObjectURL(blob as Blob); await downloadAnnotationsUsingGet(
const a = document.createElement("a"); projectId,
a.href = url; values.format,
a.download = filename; values.onlyAnnotated,
document.body.appendChild(a); values.includeData,
a.click(); filename
window.URL.revokeObjectURL(url); );
document.body.removeChild(a);
message.success("导出成功"); message.success("导出成功");
onClose(); onClose();

View File

@@ -109,12 +109,13 @@ export function downloadAnnotationsUsingGet(
projectId: string, projectId: string,
format: ExportFormat = "json", format: ExportFormat = "json",
onlyAnnotated: boolean = true, onlyAnnotated: boolean = true,
includeData: boolean = false includeData: boolean = false,
filename?: string
) { ) {
const params = new URLSearchParams({ const params = new URLSearchParams({
format, format,
only_annotated: String(onlyAnnotated), only_annotated: String(onlyAnnotated),
include_data: String(includeData), include_data: String(includeData),
}); });
return download(`/api/annotation/export/projects/${projectId}/download?${params.toString()}`); return download(`/api/annotation/export/projects/${projectId}/download?${params.toString()}`, null, filename);
} }