python2 接口下载文件,文件名设置中文无效解决方案

from urllib import quote
_file_name = quote("测试".encode("utf-8"))
data = models.AdaptationPChannel.export_active_project(start_date)
response = HttpResponse(data)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = u'attachment;filename="%s"' % _file_name

  由于字符串默认编码方式为unicode需要转utf-8然后通过quote转urlencode。 
"测试" 转码后  "%E6%B5%8B%E8%AF%95"

posted @ 2022-10-14 19:58  你看起来真的很好吃  阅读(57)  评论(0编辑  收藏  举报