Return File

29 views
Skip to first unread message

Jason Macgowan

unread,
Oct 11, 2012, 8:48:02 PM10/11/12
to we...@googlegroups.com
Hello,

I have a web app that accepts requests, and based on the request it builds a zip file.  How can I get web.py to yield this file?

I tried something along the lines of

def GET(self):
    return open('/path/to/file')

But that doesn't work, and is probably not even close to the right direction to go.

Any advice?

Andrey Kuzmin

unread,
Oct 12, 2012, 7:58:30 AM10/12/12
to we...@googlegroups.com
Hi, this is how I stream the file:

    def GET(self):
        # assuming we have selected doc from the database
        web.header("Content-Disposition", "attachment; filename=%s" % doc.filename)
        web.header("Content-Type", doc.filetype)
        web.header('Transfer-Encoding','chunked')
        f = open(doc.filename, 'rb')
        while 1:
            buf = f.read(1024 * 8)
            if not buf:
                break
            yield buf
Reply all
Reply to author
Forward
0 new messages