Serving pdf files by link, downloaded as pdf.html

Question:

Crated function to allow users download pdf files by link. Works fine, the only problem that what user save is .html. So all files are 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

Why?

Asked By: user3475724

||

Answers:

You should move content_type into HttpResponse(pdf.read(), content_type='application/pdf'), it’s an attribute of HttpResponse

Answered By: Thomas Jiang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.