0

I have this code

class DownloadView(TemplateView):
    template_name = 'pdfform/create_form2.html'


    def serve_pdf(self, request):
        #pdf_data = magically_create_pdf()

        response = HttpResponse(mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="http://localhost/static/pdfs/angular.pdf"'
        return response

When i go to that page i get the downlaod dialog but i am not able to download the file. it says that

http 403 forbidden

Now i can directly access the file but putting http://localhost/static/pdfs/angular.pdf that in browser

i treid puting static/pdfs/angular.pdf but same error

Mirage
  • 29,790
  • 59
  • 163
  • 256
  • I think this page might be useful: http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files – Cory Walker Nov 30 '12 at 05:42

1 Answers1

1

Filename in should be just a plain file name, not http://....

So change

response['Content-Disposition'] = 'attachment; filename="http://localhost/static/pdfs/angular.pdf"'

to

response['Content-Disposition'] = 'attachment; filename="angular.pdf"'

Also, you need to serve the file content through response so that the file contents are served.

e.g

...
def serve_pdf(self, request):
  from django.core.servers.basehttp import FileWrapper
  # your code

  wrapper      = FileWrapper(open(your_pdf_file_path))
  response     = HttpResponse(wrapper,'application/pdf')
  response['Content-Length']      = os.path.getsize(your_pdf_file_path)    
  response['Content-Disposition'] = 'attachment; filename="angular.pdf"'
  return response
Rohan
  • 50,238
  • 11
  • 84
  • 85