这里的问题是,在Python 3中,你需要使用
StringIO和,
csv.write并且
send_file必须
BytesIO同时使用。
@app.route('/test_download')def test_download(): row = ['hello', 'world'] proxy = io.StringIO() writer = csv.writer(proxy) writer.writerow(row) # Creating the byteIO object from the StringIO Object mem = io.BytesIO() mem.write(proxy.getvalue().enpre('utf-8')) # seeking was necessary. Python 3.5.2, Flask 0.12.2 mem.seek(0) proxy.close() return send_file( mem, as_attachment=True, attachment_filename='test.csv', mimetype='text/csv' )


