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

2015-08-03 14:59:41 +08:00
 developerif
首先在线形成一个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')
2007 次点击
所在节点    Python
1 条回复
developerif
2015-08-06 21:52:53 +08:00
没人么?~~~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/210454

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX