V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
developerif
V2EX  ›  Python

用 django 下载文件 word,老是弹出错误,怎么办?

  •  
  •   developerif · 2015-08-03 14:59:41 +08:00 · 1999 次点击
    这是一个创建于 3188 天前的主题,其中的信息可能已经有所发展或是发生改变。
    首先在线形成一个word(用的win32com模块),另存在服务器,然后下载,代码如下,其中第四行response['Content-Encoding']:改成unicode、utf- 8、gbk都不行。
    doc.SaveAs('C:\A.doc' )
    wr=FileWrapper(open('C:\A.doc'))
    response = HttpResponse(wr,content_type='application/msword')
    response['Content-Encoding']='unicode'
    response['Content-Disposition'] = ('attachment; filename=A.doc')
    return response

    报错如下:
    UnicodeDecodeError at /
    'gbk' codec can't decode byte 0xb1 in position 5: illegal multibyte sequenceRequest Method: POST
    Request URL: http://10.101.15.211:8000/
    Django Version: 1.8.3
    Exception Type: UnicodeDecodeError
    Exception Value: 'gbk' codec can't decode byte 0xb1 in position 5: illegal multibyte sequence
    Exception Location: C:\Python34\lib\wsgiref\util.py in __next__, line 30
    Python Executable: C:\Python34\python.exe
    Python Version: 3.4.3

    下面还有一行 指向之前贴的代码第三行的:
    F:\mysite\fenfa\views.py in home
    168. response = HttpResponse(wr,content_type='application/msword')
    1 条回复    2015-08-06 21:52:53 +08:00
    developerif
        1
    developerif  
    OP
       2015-08-06 21:52:53 +08:00
    没人么?~~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5518 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:47 · PVG 14:47 · LAX 23:47 · JFK 02:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.