美文网首页
UnicodeEncodeError: ‘latin-1’ co

UnicodeEncodeError: ‘latin-1’ co

作者: butters001 | 来源:发表于2021-11-04 14:27 被阅读0次

后端server返回excel的base64时,文件名包含中文报错
发现文件名有中文名字,所以导致错误,编码是latin-1编码, 所以我们需要将文件名编码成utf-8再解码成latin-1。

self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=' + os.path.basename(path).encode("utf-8").decode("latin1"))

又遇到问题,前端从头中获取文件名,文件名乱码。放弃上面的方法,改为

from tornado.escape import url_escape
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=' + url_escape(os.path.basename(url)))

相关文章

网友评论

      本文标题:UnicodeEncodeError: ‘latin-1’ co

      本文链接:https://www.haomeiwen.com/subject/fmhkzltx.html