通过链接提供pdf文件,下载为pdf.html

时间:2017-05-09 00:27:46

标签: python django pdf flask response

允许用户通过链接下载pdf文件的Crated函数。工作正常,用户保存的唯一问题是.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

为什么?

1 个答案:

答案 0 :(得分:0)

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