Python Flask send_file StringIO blank files

前端 未结 3 1669
無奈伤痛
無奈伤痛 2020-12-13 18:42

I\'m using python 3.5 and flask 0.10.1 and liking it, but having a bit of trouble with send_file. I ultimately want to process a pandas dataframe (from Form data, which is u

3条回答
  •  我在风中等你
    2020-12-13 19:08

    I guess you should write bytes.

    from io import BytesIO    
    
    from flask import Flask, send_file
    
    
    app = Flask(__name__)
    
    
    @app.route('/test_download', methods=['POST'])
    def test_download():
        # Use BytesIO instead of StringIO here.
        buffer = BytesIO()
        buffer.write(b'jJust some letters.')
        # Or you can encode it to bytes.
        # buffer.write('Just some letters.'.encode('utf-8'))
        buffer.seek(0)
        return send_file(buffer, as_attachment=True,
                         attachment_filename='a_file.txt',
                         mimetype='text/csv')
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

提交回复
热议问题