Response attachment filename 中文乱码 headers

出现的场景:

1、后端下载文件的接口,文件名无法支持中文

。。。

解决方案: 需要对包含中文等特殊字符的文件名进行转码(js 里面是 encodeURIComponent函数,python 里面是urllib.parse.quote函数,java 里面是java.net.URLEncoder.encode 函数)操作【参考url 对特殊字符的转码操作】。

python headers 返回示例:

from urllib.parse import quote
file_name=XXX
headers = {
'Content-Disposition': f'''attachment; filename="{quote(file_name)}"'''
}

经过简单阅读代码发现: http headers 的 value 和key 部分都是需要进行latin-1编码。所以只要是latin-1无法编码的字符串,都需要进行转码操作。

posted @ 2023-01-29 17:21  xunhanliu  阅读(532)  评论(0编辑  收藏  举报