我正在尝试使用 django 1.7 提供 PDF 文件,这基本上是“应该”工作的代码...如果我将 content_type 更改为“文本”并用它下载 .tex 文件,它当然可以工作,但是当我用二进制文件尝试,得到“UnicodeDecodeError at /path/to/file/filename.pdf”
“utf-8”编解码器无法解码位置 10 中的字节 0xd0:无效的连续字节”
def download(request, file_name):
file = open('path/to/file/{}'.format(file_name), 'r')
response = HttpResponse(file, content_type='application/pdf')
response['Content-Disposition'] = "attachment; filename={}".format(file_name)
return response
所以基本上,如果我理解正确的话,它会尝试将文件作为 UTF-8 编码的文本文件,而不是二进制文件。我尝试将 content_type 更改为“application/octet-stream”,但结果类似。我缺少什么?
尝试使用二进制模式打开文件:
file = open('path/to/file/{}'.format(file_name), 'rb')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)