在利用 python HTTP 下載下傳檔案的時候(如 Django),遇到檔案名是中文的時候會遇到一些問題,比如亂碼等,在這裡記錄一下解決版本,和使用的程式設計語言沒有關系,關鍵是要把
Content-Disposition
給設定一下。需要将中文名進行 url編碼,在 python 中可以利用
urllib.parse
的
quote
函數
from urllib.parse import quote
file_name = quote(file_name)
response["Content-Disposition"] = f"attachment;filename*=utf-8''{file_name}"
或者在 go 中可以利用
url
庫的
QueryEscape
來将名字進行編碼
name = url.QueryEscape(name)
c.Response.Header.Set("Content-disposition", fmt.Sprintf("attachment;filename*=utf-8''%s", name))