如何在Django项目上实现pdf或doc文件上传和下载?

2024-09-29 22:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个网站,我可以上传一个下载页面上的目录,用户可以下载它们,我可以通过在视图中使用以下代码为单个文件实现这一点,但我需要一个更简单的方法,我可以在管理或模板上做到这一点。你知道吗

def brochure(request):
 fs = FileSystemStorage()
 filename = 'kastom_profile.pdf'
 if fs.exists(filename):
    with fs.open(filename) as pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        response['Content-Disposition'] = 'inline;filename="kastom_profile.pdf"'
        return response
  else:
    return HttpResponseNotFound('The requested pdf was not found in our server.')

Tags: 文件方法代码用户目录视图returnpdf

热门问题