Snippets are unofficial and unmaintained.
This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.
Sometimes, you want to avoid the creation of a file to send this file to the client.
So, in this case, you can use a StringIO object with the send_file helper.
Here is an example:
#!/usr/bin/env python # Thanks to Dan Jacob for a part of the code ! from flask import Flask, send_file import StringIO app = Flask(__name__) @app.route('/') def index(): strIO = StringIO.StringIO() strIO.write('Hello from Dan Jacob and Stephane Wirtel !') strIO.seek(0) return send_file(strIO, attachment_filename="testing.txt", as_attachment=True) app.run(debug=True)
This snippet by Stephane Wirtel can be used freely for anything you like. Consider it public domain.