首页 > 代码库 > Openerp上传中文名附件,下载时报错的处理方法

Openerp上传中文名附件,下载时报错的处理方法

文档管理中,如果上传的文件名含有中文字符,下载时会提示出错,如没有权限等。这个问题困惑我比较久的时间,通过跟踪openerp_server.log,可以看到类似提示:

2012-09-28 21:51:39,069 2332 INFO ? werkzeug: 127.0.0.1 - - [28/Sep/2012 21:51:39] "POST /web/binary/saveas_ajax HTTP/1.1" 500 -
2012-09-28 21:51:39,115 2332 ERROR ? werkzeug: Error on request:
Traceback (most recent call last):
  File "werkzeug\serving.py", line 159, in run_wsgi
  File "werkzeug\serving.py", line 146, in execute
  File "C:\Program Files (x86)\OpenERP 6.1-1\server\openerp\wsgi\core.py", line 397, in application
    result = handler(environ, start_response)
  File "C:\Program Files (x86)\OpenERP 6.1-1\server\openerp\addons\web\common\http.py", line 467, in __call__
    return self.dispatch(environ, start_response)
  File "werkzeug\wsgi.py", line 411, in __call__
  File "C:\Program Files (x86)\OpenERP 6.1-1\server\openerp\addons\web\common\http.py", line 505, in dispatch
    return response(environ, start_response)
  File "werkzeug\wrappers.py", line 1082, in __call__
  File "werkzeug\wrappers.py", line 1072, in get_wsgi_response
  File "werkzeug\datastructures.py", line 1141, in to_list
UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters in position 28-29: ordinal not in range(256)


    可见问题还是出在字符解析上。(附件文档相关的数据表是ir_attchement,数据表里面的中文没有问题)

    后来逛论坛,发现http://shine-it.net/index.php/topic,2919.msg9861.html#msg9861的帖子tommo提到解决办法,

 

addons/web/controllers/main.py
Binary类的saveas和saveas_ajax方法中:
在 return req.make_response(....)前面插入

if isinstance(filename, unicode): filename=filename.encode(‘utf8‘)

 

重启openerp服务。

 

    试了可以下载,但是文件名的中文部分乱码,因为考虑到文件名除了中英文外,没有其它语言,遂改为:

 

if isinstance(filename, unicode): filename=filename.encode(‘gb2312‘)