zoukankan      html  css  js  c++  java
  • 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 "werkzeugserving.py", line 159, in run_wsgi
      File "werkzeugserving.py", line 146, in execute
      File "C:Program Files (x86)OpenERP 6.1-1serveropenerpwsgicore.py", line 397, in application
        result = handler(environ, start_response)
      File "C:Program Files (x86)OpenERP 6.1-1serveropenerpaddonswebcommonhttp.py", line 467, in __call__
        return self.dispatch(environ, start_response)
      File "werkzeugwsgi.py", line 411, in __call__
      File "C:Program Files (x86)OpenERP 6.1-1serveropenerpaddonswebcommonhttp.py", line 505, in dispatch
        return response(environ, start_response)
      File "werkzeugwrappers.py", line 1082, in __call__
      File "werkzeugwrappers.py", line 1072, in get_wsgi_response
      File "werkzeugdatastructures.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')

  • 相关阅读:
    如何得到需要下载文件的链接(路径)?
    Python之内存泄漏和内存溢出
    IO多路复用
    python-socket和进程线程协程(代码展示)
    Xshell(远程)连接不上linux服务器(防火墙介绍)
    Shell--变量
    Python读写配置文件模块--Configobj
    python文件处理之fileinput
    python之commands和subprocess入门介绍(可执行shell命令的模块)
    linux下 > /dev/null 2 > &1 的意思和如何在后台启动进程
  • 原文地址:https://www.cnblogs.com/jacker1979/p/3716565.html
Copyright © 2011-2022 走看看