-2

I need to provide a button, once the user click on it It will download a PDF file or Image.

I have been tried so many ways out there. non of them works. and I couldn't find any tutorial for this topic.

john
  • 25
  • 6

1 Answers1

0

I'll copypaste a working code from my own question (Django 1.7, Python 3.4): Django 1.7: serve a pdf -file (UnicodeDecodeError)

from views.py:

from django.http import HttpResponse

def download(request, file_name):
    file = open('path/to/file/{}'.format(file_name), 'rb')
    response = HttpResponse(file, content_type='application/pdf')
    response['Content-Disposition'] = "attachment; filename={}".format(file_name)
    return response
Community
  • 1
  • 1
Joonas Joensuu
  • 102
  • 2
  • 10
  • can you show me the related code in settins.py please? – john Feb 01 '15 at 10:49
  • I will get PermissionError – john Feb 01 '15 at 10:58
  • My settings.py isn't anything special - I just added my app to the list of apps. If you get PermissionError, the first thing I'd look in is your file directory (I created a temp directory with +777 -access, which is probably not a good idea as such - at least you should remove the executable -rights). Are you running the server on your own computer or a remote hosting service? Also, just stating "PermissionError" isn't informative enough. – Joonas Joensuu Feb 02 '15 at 13:05