在屏幕上显示docx文件

时间:2018-05-09 19:12:03

标签: django

我在django中使用MailMerge创建了一个ms-word文档。它运作正常。 现在,我想在屏幕上显示此文件。我写了下面的代码,但它不起作用。

views.py

with open(file_path) as doc:
   response = HttpResponse(doc.read(), content_type='application/ms-word')
   response = HttpResponse(template_output)
   response['Content-Disposition'] = 'attachment;filename=var_nomdocumento_output'

错误:

[Errno 13] Permission denied: 'C:\\GROWTHTECH\\Projetos\\blockchain\\media_root/procuracao'

1 个答案:

答案 0 :(得分:0)

您忘记提供二进制打开模式。它可以r打开以供阅读(默认)w打开进行写入,先截断文件,b进行二进制模式。

所以在我们的案例中:它将是rb

file_path = 'path/path/file.docx'
with open(file_path,'rb') as doc:
   response = HttpResponse(doc.read(), content_type='application/ms-word')
   # response = HttpResponse(template_output)
   response['Content-Disposition'] = 'attachment;filename=name.docx'
   return response

据我所知,目前没有浏览器呈现Word文档。因此,无论参数是'attachment;filename="file_name"'还是'inline;filename="file_name"'

,您的文件都会自动下载
相关问题