[Django]-Download a file in django with a button using xmlhttprequest

6👍

In your view, I suggest adding a size to your response

content = file(filename).read()
response = HttpResponse(content, content_type='text/plain')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'my_pdf.pdf'

As @bob-vork just said, the filename from Content-Disposition should not be the python file object, but the name your download will have.

How about registering the view in urls.py then replace the javascript by:

<input type="button" value="Download" onclick="window.open('download_my_pdf')">

EDIT :

Since you’re going with the suggested HTML, then I’ll put more details to my solution.

First, you can drop the home.js. As you understood

<a href="javascript:void(0)" class="btn btn-primary btn-lg dump">Download</a>

becomes

<input type="button" value="Download" onclick="window.open('download_my_pdf')">

your views.py should return directly the response

from django.http import HttpResponse
from wsgiref.util import FileWrapper

@login_required
def download_pdf(request):
    filename = 'whatever_in_absolute_path__or_not.pdf'
    content = FileWrapper(filename)
    response = HttpResponse(content, content_type='application/pdf')
    response['Content-Length'] = os.path.getsize(filename)
    response['Content-Disposition'] = 'attachment; filename=%s' % 'whatever_name_will_appear_in_download.pdf'
    return response

Pay attention to put the right content_type as well. Note that I haven’t tested the view (especially the FileWrapper), I adapted some code I had for plain text. This is also why you have a TypeError if you treat the pdf as a text file.

In urls.py

urlpatterns = (
    [...]
    url('download_my_pdf', download_pdf),
)

0👍

In your view, you set Content-Disposition as attachment; filename=... but you append the file object. My guess is the browser might want a filename, preferably with the right extension.

I have a similar view in a Django project, and I’m quoting the filename as well, but I’m not sure if that is necessary

EDIT: also, there is a typo in “attachment”, remove the extra “e” and see if that helps

Leave a comment