fix: 使用标准方法编码下载文件名

This commit is contained in:
jerryliang
2026-01-16 17:18:14 +08:00
parent 442ef5b0e0
commit ff1943d60c
+10 -5
View File
@@ -354,8 +354,9 @@ class S3FileStorage(FileStorageInterface):
yield chunk yield chunk
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
encoded_filename = quote(filename, safe='')
headers = { headers = {
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"', "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
"Content-Length": str(content_length) "Content-Length": str(content_length)
} }
return StreamingResponse( return StreamingResponse(
@@ -671,8 +672,9 @@ class OneDriveFileStorage(FileStorageInterface):
break break
yield chunk yield chunk
encoded_filename = quote(filename, safe='')
headers = { headers = {
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"', "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
"Content-Length": str(content_length) "Content-Length": str(content_length)
} }
return StreamingResponse( return StreamingResponse(
@@ -862,8 +864,9 @@ class OpenDALFileStorage(FileStorageInterface):
except AttributeError: except AttributeError:
# 如果 reader 方法不存在,回退到全量读取(兼容旧版本) # 如果 reader 方法不存在,回退到全量读取(兼容旧版本)
content = await self.operator.read(await file_code.get_file_path()) content = await self.operator.read(await file_code.get_file_path())
encoded_filename = quote(filename, safe='')
headers = { headers = {
"Content-Disposition": f'attachment; filename="{filename}"', "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
"Content-Length": str(content_length) "Content-Length": str(content_length)
} }
return Response( return Response(
@@ -878,8 +881,9 @@ class OpenDALFileStorage(FileStorageInterface):
break break
yield chunk yield chunk
encoded_filename = quote(filename, safe='')
headers = { headers = {
"Content-Disposition": f'attachment; filename="{filename}"', "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
"Content-Length": str(content_length) "Content-Length": str(content_length)
} }
return StreamingResponse( return StreamingResponse(
@@ -1105,8 +1109,9 @@ class WebDAVFileStorage(FileStorageInterface):
break break
yield chunk yield chunk
encoded_filename = quote(filename, safe='')
headers = { headers = {
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode()}"', "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
"Content-Length": str(content_length) "Content-Length": str(content_length)
} }
return StreamingResponse( return StreamingResponse(