link 提供 pdf 文件,下载为 pdf.html

Serving pdf files by link, downloaded as pdf.html

已创建允许用户通过 link 下载 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

为什么?

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

attribute