How to download a file when a django function is called by javascript instead of navigation to the url?

2,704 views
Skip to first unread message

Joel Mathew

unread,
Nov 17, 2018, 12:10:51 AM11/17/18
to django...@googlegroups.com
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

Jason

unread,
Nov 17, 2018, 8:46:18 AM11/17/18
to Django users

Joel Mathew

unread,
Nov 17, 2018, 9:27:03 AM11/17/18
to django...@googlegroups.com
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



On Sat, 17 Nov 2018 at 19:16, Jason <jjohn...@gmail.com> wrote:

--
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.

Jason

unread,
Nov 17, 2018, 9:38:28 AM11/17/18
to Django users
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.

Yavin Aalto Arba

unread,
Nov 17, 2018, 10:20:32 AM11/17/18
to django...@googlegroups.com
did you try to set "as_attachment=True" ?

Joel Mathew

unread,
Nov 17, 2018, 10:22:14 AM11/17/18
to django...@googlegroups.com
Where?
Sincerely yours,

Joel G Mathew



On Sat, 17 Nov 2018 at 20:50, Yavin Aalto Arba <yavi...@gmail.com> wrote:
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.

Jason

unread,
Nov 17, 2018, 10:50:26 AM11/17/18
to Django users
oh, I didn't know that was a thing.  TIL.


note the bit about as_attachment setting content disposition

Joel

unread,
Nov 17, 2018, 10:55:18 AM11/17/18
to django...@googlegroups.com
Yes. But it's not usable when we're using JavaScript to fetch the url.

--
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.

Yavin Aalto Arba

unread,
Nov 17, 2018, 11:03:58 AM11/17/18
to Django users
will work with window.location=  

Andréas Kühne

unread,
Nov 17, 2018, 12:44:36 PM11/17/18
to django...@googlegroups.com
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


Reply all
Reply to author
Forward
0 new messages