fix(annotation): 解决文件名中文编码问题

- 添加 urllib.parse.quote 用于文件名编码
- 实现 RFC 5987 标准支持 UTF-8 编码的文件名
- 修改 Content-Disposition 头部使用 filename* 参数
- 确保中文文件名在下载时正确显示
This commit is contained in:
2026-01-19 14:23:14 +08:00
parent acc284761f
commit e192c826eb

View File

@@ -11,6 +11,8 @@
from __future__ import annotations from __future__ import annotations
from urllib.parse import quote
from fastapi import APIRouter, Depends, Path, Query from fastapi import APIRouter, Depends, Path, Query
from fastapi.responses import Response from fastapi.responses import Response
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -79,11 +81,15 @@ async def download_annotations(
request=request, request=request,
) )
# RFC 5987: 使用 filename* 支持 UTF-8 编码的文件名
encoded_filename = quote(filename, safe="")
content_disposition = f"attachment; filename*=UTF-8''{encoded_filename}"
return Response( return Response(
content=content, content=content,
media_type=content_type, media_type=content_type,
headers={ headers={
"Content-Disposition": f'attachment; filename="{filename}"', "Content-Disposition": content_disposition,
"Content-Length": str(len(content)), "Content-Length": str(len(content)),
}, },
) )