Hi, I'm ThadeusB.

I code stuff. I raise bees. I game.

Download of a file-like python object in web2py

Here are a couple of ways to expose any python file-like object as download-able in a web2py controller.

You might want to do this if you have a generated pdf report, or allowing an export of the sites content.

From your controller, you can stream a large file by using the stream function of the response object.

return response.stream(filelikeobject, chunk_size=64*1024)

You might also want to set the content type of what you are returning, as well as a new disposition (filename).

def export():
    from gluon.contenttype import contenttype
    response.headers['Content-Type'] = contenttype('.csv')
    response.headers['Content-disposition'] = 'attachment; filename=%s_database.csv' % (
        request.now
    )
    import csv, cStringIO
    s = cStringIO.StringIO()
    db.export_to_csv_file(s, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
    return s.getvalue()