Joel Mathew
2018-11-17 05:09:44 UTC
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
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.
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.