fix file name download issue
frdel committed
Nov 20, 2025 at 13:25 UTC
94cfa49d87e9bad70b5f4da8fd484f210db4a6cb
1 file changed
+12
-1
python/api/download_work_dir_file.py
+12
-1
@@ -7,6 +7,8 @@ from flask import Response
7
from python.helpers.api import ApiHandler, Input, Output, Request
8
from python.helpers import files, runtime
9
from python.api import file_info
10
+from urllib.parse import quote
11
+
12
13
14
def stream_file_download(file_source, download_name, chunk_size=8192):
@@ -63,7 +65,7 @@ def stream_file_download(file_source, download_name, chunk_size=8192):
65
content_type=content_type,
66
direct_passthrough=True, # Prevent Flask from buffering the response
67
headers={
66
- 'Content-Disposition': f'attachment; filename="{download_name}"',
68
+ 'Content-Disposition': make_disposition(download_name),
69
'Content-Length': str(file_size), # Critical for browser progress bars
70
'Cache-Control': 'no-cache',
71
'X-Accel-Buffering': 'no', # Disable nginx buffering
@@ -74,6 +76,15 @@ def stream_file_download(file_source, download_name, chunk_size=8192):
76
return response
77
78
79
+def make_disposition(download_name: str) -> str:
80
+ # Basic ASCII fallback (strip or replace weird chars)
81
+ ascii_fallback = download_name.encode("ascii", "ignore").decode("ascii") or "download"
82
+ utf8_name = quote(download_name) # URL-encode UTF-8 bytes
83
+
84
+ # RFC 5987: filename* with UTF-8
85
+ return f'attachment; filename="{ascii_fallback}"; filename*=UTF-8\'\'{utf8_name}'
86
+
87
+
88
class DownloadFile(ApiHandler):
89
90
@classmethod