Discussion:
How to download a file when a django function is called by javascript instead of navigation to the url?
Joel Mathew
2018-11-17 05:09:44 UTC
Permalink
I have a page where several buttons process different functions like
sending a user sms (via API), sends an file by email, or downloads a PDF
file. Button actions dont use forms, but uses ajax requests via javascript.

I used to create a pdf file using javascript (jspdf), but have written code
which generates a pdf file by python on the server. Now I need to allow
download when the button is clicked.

Server code snippet:

with NamedTemporaryFile(mode='w+b') as temp:
from django.http import FileResponse
doc = SimpleDocTemplate(temp.name, pagesize=A4, rightMargin=20,
leftMargin=20, topMargin=20, bottomMargin=20, allowSplitting=1,
title="Prescription", author="MyOPIP.com")
doc.build(elements)
print(f'Generated {temp.name}')
return FileResponse(open(temp.name, 'rb'),
content_type='application/pdf')

The above code is supposed to download a pdf file, if called ny navigating
to the url.

On my javascript side, I tried to determine what I'm receiving:

$.ajax({
url:
`/clinic/${cliniclabel}/prescription/download/patient/${patient_id}`,
dataType: "html",
data: data,
type: 'POST',
success: function (data) {
console.log("Received data from server..type is ", typeof(data))
console.log(data)
}
});

And I get:

Received data from server..type is string
userjs/main.js:2067 %PDF-1.4
%ᅵᅵᅵᅵ ReportLab Generated PDF document http://www.reportlab.com
1 0 obj
<<
/F1 2 0 R /F2 5 0 R
endobj
2 0 obj....

Apparently django sends this as a file stream. How can I get the file
downloaded to the user when the response is processed by javascript?
Sincerely yours,

Dr Joel G Mathew
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_9S4%2B-AoZ56FdvaCrDMh5KQyPL%3D2hKi-eBXZAQgYMEDqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-11-17 13:46:18 UTC
Permalink
check out
https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/181a3c12-8b08-4220-b95d-b6fc02002c18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Joel Mathew
2018-11-17 14:25:47 UTC
Permalink
Hi Jason,
Thank you for responding.
I solved this by implementing a temporary link system in my model. This
would generate a unique key which points to a foreign key which references
the data I want. When the url with this foreign key is clicked, the pdf is
generated on the fly and presented as a FileResponse. On the javascript
side, what I do is generate the link when the button is clicked, and insert
a Download button in the DOM and the href is set to the link presented by
django.

Sincerely yours,

Dr Joel G Mathew
Post by Jason
check out
https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/181a3c12-8b08-4220-b95d-b6fc02002c18%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/181a3c12-8b08-4220-b95d-b6fc02002c18%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw__crkp1_SAt%2Bugm_jpQ2NfpwcBQ68dv6foN6rLPreMWNQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-11-17 14:38:27 UTC
Permalink
not a bad solution, but you could most likely make the link
generator/decoder work in your view handler rather than needing to hit the
database. That would prevent the necessity of some method to clean up the
db of old temp links that are no longer valid.
Post by Joel Mathew
Hi Jason,
Thank you for responding.
I solved this by implementing a temporary link system in my model. This
would generate a unique key which points to a foreign key which references
the data I want. When the url with this foreign key is clicked, the pdf is
generated on the fly and presented as a FileResponse. On the javascript
side, what I do is generate the link when the button is clicked, and insert
a Download button in the DOM and the href is set to the link presented by
django.
Sincerely yours,
Dr Joel G Mathew
Post by Jason
check out
https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/181a3c12-8b08-4220-b95d-b6fc02002c18%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/181a3c12-8b08-4220-b95d-b6fc02002c18%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cbce15cb-a91a-4174-ac87-b595372a3320%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yavin Aalto Arba
2018-11-17 09:48:38 UTC
Permalink
did you try to set "as_attachment=True" ?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%2Be-ZWVCysVfPG5iHS_Mr9XgASV_JCPnfj74Yu3edLQ96RNow%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Joel Mathew
2018-11-17 15:21:02 UTC
Permalink
Where?
Sincerely yours,

Joel G Mathew
Post by Yavin Aalto Arba
did you try to set "as_attachment=True" ?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2B%2Be-ZWVCysVfPG5iHS_Mr9XgASV_JCPnfj74Yu3edLQ96RNow%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CA%2B%2Be-ZWVCysVfPG5iHS_Mr9XgASV_JCPnfj74Yu3edLQ96RNow%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_9OKhi7xzZyd2sO3njck_gsmkWTCK0ECPOVXc2C-B7REA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-11-17 15:50:25 UTC
Permalink
oh, I didn't know that was a thing. TIL.

https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.FileResponse

note the bit about as_attachment setting content disposition
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Joel
2018-11-17 15:54:26 UTC
Permalink
Yes. But it's not usable when we're using JavaScript to fetch the url.
Post by Jason
oh, I didn't know that was a thing. TIL.
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.FileResponse
note the bit about as_attachment setting content disposition
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_8mx5-wsdR39naUt%2BPYiTCGP5dD%2BMCJ8QObB9%2BLGxGqrg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Yavin Aalto Arba
2018-11-17 16:03:58 UTC
Permalink
will work with window.location=
Post by Joel
Yes. But it's not usable when we're using JavaScript to fetch the url.
Post by Jason
oh, I didn't know that was a thing. TIL.
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.FileResponse
note the bit about as_attachment setting content disposition
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8e08441c-3efd-4fe8-837d-26612ab0d288%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andréas Kühne
2018-11-17 17:43:41 UTC
Permalink
If you use a windows.location solution, you will get the native solution
for downloading files in the browser - which usually is to download the
file to the downloads location (if you use attachment=True). I would prefer
that than to having to create the file and link to it, or to save it via JS
(which is also possible).

Regards,

Andréas
Post by Joel
Yes. But it's not usable when we're using JavaScript to fetch the url.
Post by Jason
oh, I didn't know that was a thing. TIL.
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.FileResponse
note the bit about as_attachment setting content disposition
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/a950f7f3-2e7e-490d-b271-3125829a9376%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAA%3Diw_8mx5-wsdR39naUt%2BPYiTCGP5dD%2BMCJ8QObB9%2BLGxGqrg%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_8mx5-wsdR39naUt%2BPYiTCGP5dD%2BMCJ8QObB9%2BLGxGqrg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCc2kPdm8MFQ6BEBzFxAikKTd2VVsbfKCWeCVgt4bO6a%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...