首页 > 其他分享 >Response attachment filename 中文乱码 headers

Response attachment filename 中文乱码 headers

时间:2023-01-29 17:34:17浏览次数:47  
标签:中文 转码 quote filename 乱码 headers attachment

出现的场景:

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无法编码的字符串,都需要进行转码操作。

标签:中文,转码,quote,filename,乱码,headers,attachment
From: https://www.cnblogs.com/xunhanliu/p/17073285.html

相关文章