Downloading a file on the fly from Flask+sqlalchemy

Following code snippet shows how to implement downloading a file with flask.

def index():
    strIO = StringIO.StringIO()
    strIO.write('Hello from Dan Jacob and Stephane Wirtel !')
    return send_file(strIO,

In case you need to use the snippet to support downloading a file which is stored as BLOB originally and to convert it as a downloadable file stream on the fly, then you can use io.BytesIO. See the below:

Let’s say exampleModel is the Sqlalchemy model object and blobField is a blob field which stores blob binary data,

buffer = io.BytesIO(exampleModel.blobField)    // You don’t need this
return send_file(buffer, attachment_filename=filename)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s