推荐学习书目
› 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 · Aug 3, 2015 · 2888 views
    This topic created in 4070 days ago, the information mentioned may be changed or developed.
    首先在线形成一个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 replies  •  2015-08-06 21:52:53 +08:00
    developerif
        1
    developerif  
    OP
       Aug 6, 2015
    没人么?~~~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2524 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 01:44 · PVG 09:44 · LAX 18:44 · JFK 21:44
    ♥ Do have faith in what you're doing.