0

私はこのコードを持っています

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

そのページに移動すると、ダウンロード ダイアログが表示されますが、ファイルをダウンロードできません。それは言う

http 403 forbidden

今、私はファイルに直接アクセスできますが、http://localhost/static/pdfs/angular.pdfそれをブラウザに入れます

私は入れ てみましstatic/pdfs/angular.pdfたが、同じエラーです

4

1 に答える 1

1

のファイル名は、ではなく、単なるファイル名である必要がありますhttp://...

だから変更

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

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

また、ファイルの内容が提供されるように、応答を通じてファイルの内容を提供する必要があります。

例えば

...
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
于 2012-11-30T05:31:43.893 に答える