E
E
Egor2018-09-21 16:48:59
Django
Egor, 2018-09-21 16:48:59

How to prevent downloaded files from being opened in the browser?

There is a view that returns a file:

def dowlnd(request, doc_id):

    doc_f = Document.objects.get(id=doc_id).file

    response = HttpResponse(doc_f)
    response['Content-Disposition'] = 'attachment; filename='+doc_f.name

    return response

When I link dowlnd/<doc_id>/, I get a file. The image format is downloaded normally, that is, it is immediately saved by the browser to the download folder. But the docx format, the browser tries to open in a new window. As always, should I download any file format so that the browser does not try to open it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question