Created
February 17, 2017 19:07
-
-
Save Miserlou/fcf0e9410364d98a853cb7ff42efd35a to your computer and use it in GitHub Desktop.
Flask serving binary data example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
from flask import Flask, send_file | |
app = Flask(__name__) | |
@app.route('/logo.jpg') | |
def logo(): | |
"""Serves the logo image.""" | |
with open("logo.jpg", 'rb') as bites: | |
return send_file( | |
io.BytesIO(bites.read()), | |
attachment_filename='logo.jpeg', | |
mimetype='image/jpg' | |
) |
I had to add as_attachment=True to send_file to get it to work.
But thanks!
Hi!! Thanks for the code!!
I have a similar code in which multiple files are uploaded and then after processing, I have a string name "b64_encoded_string" as output now this has not to be saved anywhere but to return that string to the web app and download there as .pem file. Earlier I was using without flask and saving it via - pickle.dump( b64_encoded_string, open("license.pem", "wb")).
Instead, I don't want to save anything in the web app and push all the string and download it for the user as .pem file in-app.
If anybody can help me with this, it would be a great help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@carvendy
http://flask.pocoo.org/snippets/118/