flask note

导出csv

 

from flask import Response, send_file

        import csv

        try:

            from cStringIO import StringIO

        except ImportError:

            from StringIO import StringIO

 

        return_fileobj = StringIO()

 

        writer = csv.writer(return_fileobj)

        writer.writerow("1,2,3")

        response = Response(return_fileobj.getvalue(), mimetype='text/csv')

        response.headers['Content-Disposition']='attachment; filename=test.csv'

return reponse

 

        return_fileobj.seek(0)

        return send_file(return_fileobj,as_attachment=True,attachment_filename='text.csv')

你可能感兴趣的:(Note)