【问题标题】:Serving pdf files by link, downloaded as pdf.html通过链接提供 pdf 文件,下载为 pdf.html
【发布时间】:2017-10-07 03:18:38
【问题描述】:

Crated 功能允许用户通过链接下载 pdf 文件。工作正常,用户保存的唯一问题是 .html。所以所有文件都是file.pdf.html。

def download(request,ticket_id):
    ticket_path = str(Ticket.objects.get(id=ticket_id).upload)
    with open('files/media/' + ticket_path, 'rb') as pdf:
        response = HttpResponse(pdf.read())
        response['content_type'] = 'application/pdf'
        response['Content-Disposition'] = 'attachment;filename="file.pdf"'
        return response

为什么?

【问题讨论】:

    标签: python django pdf flask response


    【解决方案1】:

    您应该将content_type 移动到HttpResponse(pdf.read(), content_type='application/pdf'),它是HttpResponse 的attribute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多