Python - 如何使用unicode文件名从文件系统发送文件? [英] Python - how to send file from filesystem with a unicode filename?

查看:280
本文介绍了Python - 如何使用unicode文件名从文件系统发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用Flask来提供一些文件。我最近把这个项目从Python 3降级到了Python 2.7,所以它可以和更多的扩展一起工作,并遇到了我以前没有遇到的问题。我尝试从文件系统提供日文文件名的文件,当我尝试返回send_from_directory(new_folder_path,文件名,as_attachment = True)
我得到 UnicodeEncodeError:'ascii'编解码器不能对位置15-20中的字符进行编码:序号不在范围内(128)。在 quote_header_value = str(value)(这是一个werkzeug事物)。
我有模板设置显示页面上的文件名,只要在{{文件名}}在HTML中,它显示得很好,所以我假设它以某种方式从文件系统读取名称?只有当我尝试send_from_directory所以用户可以下载它会抛出这个错误。我尝试了一堆 .encode('utf-8')和 .decode('utf-8')的组合。对此感到非常沮丧。在Python 3中,所有东西都是无缝工作的,因为所有的东西都被当作unicode来处理,而寻找解决这个问题的方法带来了一些结果,似乎我需要一个程序来包装我的头。有没有人有这个问题的解决办法?
谢谢。

解决方案

好了,在引擎盖下摔跤了一段时间之后我就修好了,不得不修改一些werkzeug东西的来源。在http.py中,我用 unicode(value)替换了 str(value)在http.py和datastructures.py中都带有utf-8的latin-1。它解决了这个问题,文件在最新的Firefox和Chrome中都得到了很好的下载。正如我之前所说,我宁愿不必修改我使用的库的来源,因为这是在不同的系统上部署/测试时很痛苦,所以如果任何人有更好的解决这个问题,请分享。我见过一些人建议只是使URL的文件名的一部分,但我不能这样做,因为我需要保持我的网址简单和干净。

So I am using Flask to serve some files. I recently downgraded the project from Python 3 to Python 2.7 so it would work with more extensions, and ran into a problem I did not have before. I am trying to serve a file from the filesystem with a Japanese filename, and when I try return send_from_directory(new_folder_path, filename, as_attachment=True) I get UnicodeEncodeError: 'ascii' codec can't encode characters in position 15-20: ordinal not in range(128). in quote_header_value = str(value) (that is a werkzeug thing). I have template set to display the filename on the page by just having {{filename}} in the HTML and it is displaying just fine, so I'm assuming it is somehow reading the name from the filesystem? Only when I try send_from_directory so the user can download it does it throw this error. I tried a bunch of combinations of .encode('utf-8') and.decode('utf-8')`none of which worked at all and I'm getting very frustrated with this. In Python 3 everything just worked seamlessly because everything was treated as unicode, and searching for a way to solve this brought up results that it seems I would need a degree in compsci to wrap my head around. Does anyone have a fix for this? Thanks.

解决方案

OK, after wrestling with it under the hood for a while I fixed it, but not in a very elegant way, I had to modify the source of some werkzeug things. In "http.py", I replaced str(value) with unicode(value), and replaced every instance of "latin-1" with "utf-8" in both http.py and datastructures.py. It fixed the problem, file gets downloaded fine in both the latest Firefox and Chrome. As I said before, I would rather not have to modify the source of the libraries I am using because this is a pain when deploying/testing on different systems, so if anyone has a better fix for this please share. I've seen some people recommend just making the filename part of the URL but I cannot do this as I need to keep my URLs simple and clean.

这篇关于Python - 如何使用unicode文件名从文件系统发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆