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