Thứ Ba, 24 tháng 7, 2012

Download of a file-like python object in web2py

Copy from:http://thadeusb.com/weblog/2010/1/17/download_of_a_filelike_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.
Select
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).
Select
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()

Không có nhận xét nào:

Đăng nhận xét