python - Downloading file from FileField in Django with a HTTP link in a HTML file -
i create link when user press it, download pdf file media folder in django users machine.
i tried different methods wrong me. tells me file can not found, or code running file corrupted.
my html link:
<td> <a href="/download/">download</a></td>
my url pattern links view:
url(r'^download/$', views.downloadpdf),
my filefield this:
upload_pdf = models.filefield()
following snippet code view downloads corrupted pdf:
def downloadpdf(request): filename = '/home/user/pycharmprojects/myproject/media/invoice_template.pdf' response = httpresponse(content_type='application/pdf') fileformat = "pdf" response['content-disposition'] = 'attachment; filename=thisismypdf'.format(fileformat) return response
so, have make working ?
with open(os.path.join(settings.media_root, 'invoice_template.pdf'), 'rb') fh: response = httpresponse(fh.read(), content_type="application/pdf") response['content-disposition'] = 'attachment; filename=invoice.pdf' return response
Comments
Post a Comment