returning a zip file for download

2,021 views
Skip to first unread message

Tomas Neme

unread,
Jul 31, 2012, 6:03:21 PM7/31/12
to django...@googlegroups.com

Down here's the code in which I'm creating a zip file with a bunch of pdf labels, and returning it on the HttpResponse for the user to DL it.

The weird thing is that I'm getting just a pdf file (ok, I'm testing with a single file, so I don't know if that's the problem) and I can't set the downloaded filename to anything (it's always "download")

Can someone see what I'm doing wrong?

        files = []
        for detail in queryset:
            for parcel in detail.parceldescription_set.select_related().all():
                shipment = parcel.shipment
                if not shipment.label:
                    try:
                        shipment.download_label(args['username'], args['password'])
                    except Shipment.Wait:
                        self.message_user(_("Failed downloading label for "
                                            "shipment {id} because the "
                                            "Canada Post server is busy, "
                                            "please wait a couple of minutes "
                                            "and try again").format(
                            id=shipment.id))
                files.append(shipment.label.file)

        tmp = tempfile.mkstemp(suffix=".zip")
        tf = zipfile.ZipFile(tmp[1], mode="w")
        for file in files:
            filename = os.path.basename(file.name)
            tf.write(file.name, filename)
        tf.close()

        return HttpResponse(open(tmp[1]), mimetype="application/zip")


--
"The whole of Japan is pure invention. There is no such country, there are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Nikolas Stevenson-Molnar

unread,
Jul 31, 2012, 6:26:01 PM7/31/12
to django...@googlegroups.com
I'm not sure why you're getting a PDF file back, but as for the filename, you can set it using the Content-Disposition header. E.g:

response = return HttpResponse(open(tmp[1]), mimetype="application/zip")
response['Content-Disposition'] = 'attachment; filename="whatever.zip'
return response


_Nik


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Javier Guerra Giraldez

unread,
Jul 31, 2012, 6:47:25 PM7/31/12
to django...@googlegroups.com
On Tue, Jul 31, 2012 at 5:03 PM, Tomas Neme <lacry...@gmail.com> wrote:
> The weird thing is that I'm getting just a pdf file (ok, I'm testing with a
> single file, so I don't know if that's the problem)

shoot in the dark: maybe the browser is interpreting the zipped PDF as
a zip-encoded response, and decoding it for you.

the right mimetype and content-disposition (as suggested by Nikolas) should help

--
Javier

Tomas Neme

unread,
Aug 1, 2012, 1:33:45 PM8/1/12
to django...@googlegroups.com

OK, I got it working, I'm not sure what the problem was, but calling the loop variable 'file' was overriding the python default file object class

now I'm doing this:

        response = HttpResponse(File(file(tmp[1])), mimetype="application/zip")
        response['Content-disposition'] = ('attachment; '
                                           'filename="{}"').format(os.path.basename(tmp[1]))
        return response

and I'm wondering, what happens with that File object after the response is sent? when does it get closed? isn't there some cleanup code missing?

Iago Otero

unread,
Feb 20, 2018, 5:36:02 PM2/20/18
to Django users
and with puttin in de <a href="{{ YOUROBJECT.OBJECTLINKTOZIPFILE }}">.................would it work???.....because if you have a def on views.py with http response for the download....and the correct url.py..should work

Ryan Nowakowski

unread,
Feb 20, 2018, 5:55:15 PM2/20/18
to django...@googlegroups.com
To set the download filename, you'll need to set the response
Content-Disposition header[1] in your view:

response['Content-Disposition'] = 'attachment; filename="somefilename.zip"'

[1] https://docs.djangoproject.com/en/2.0/howto/outputting-pdf/#write-your-view
> --
> 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/3ca18f90-2bd2-42fd-a27b-faf1cbd570ed%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages