Wagtail CMS 的默认配置似乎是让文档链接触发文档的自动下载,而不是在浏览器中显示文档。有没有一种简单的方法可以更改此配置?
继上面LB接受的答案之后,以下代码将允许您提供要查看的文档,而无需依赖 Google 文档查看器。
您还可以通过传递查询字符串参数来恢复标准行为,例如 https://.../?download=True
from django.http import HttpResponse
from wagtail.core import hooks
@hooks.register('before_serve_document')
def serve_pdf(document, request):
if document.file_extension != 'pdf':
return # Empty return results in the existing response
response = HttpResponse(document.file.read(), content_type='application/pdf')
response['Content-Disposition'] = 'filename="' + document.file.name.split('/')[-1] + '"'
if request.GET.get('download', False) in [True, 'True', 'true']:
response['Content-Disposition'] = 'attachment; ' + response['Content-Disposition']
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1753 次 |
| 最近记录: |