In controller, forxample default.py:
def export_csv():
animals = db().select(db.purchase_type.ALL)
fname='hello.csv'
return dict(animals1=animals, fname=fname)
In view: view/default folder:
make flie export_csv.csv:
{{
import cStringIO
stream=cStringIO.StringIO()
animals1.export_to_csv_file(stream)
response.headers['Content-Type']='application/vnd.ms-excel'
response.headers['Content-disposition']='attachment;filename='+fname
response.write(stream.getvalue(), escape=False)
}}
Save to system file?, simple:
f = open('hello.csv', 'w')
f.write(stream.getvalue())
f.close()
Enjoy!
Hiển thị các bài đăng có nhãn Web2py. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Web2py. Hiển thị tất cả bài đăng
Thứ Năm, 30 tháng 8, 2012
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.
You might also want to set the content type of what you are returning, as well as a new disposition (filename).
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()
Đăng ký:
Bài đăng (Atom)