Create a response object with the data and then set the content type header. Set the content disposition header to attachment if you want the browser to save the file instead of displaying it.

@app.route('/images/<int:pid>.jpg')
def get_image(pid):
    image_binary = read_image(pid)
    response = make_response(image_binary)
    response.headers.set('Content-Type', 'image/jpeg')
    response.headers.set(
        'Content-Disposition', 'attachment', filename='%s.jpg' % pid)
    return response

Relevant: werkzeug.Headers and flask.Response

You can pass a file-like object and the header arguments to send_file to let it set up the complete response. Use io.BytesIO for binary data:

return send_file(
    io.BytesIO(image_binary),
    mimetype='image/jpeg',
    as_attachment=True,
    download_name='%s.jpg' % pid)

Prior to Flask 2.0, download_name was called attachment_filename.

Answer from dav1d on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 57844257 › why-is-binary-data-posted-to-flask-app-different
python - Why is binary data posted to flask app different? - Stack Overflow
September 9, 2019 - import requests import os data = None with open("infile.pdf", "rb") as infile: data = infile.read() print("length of input data: {}".format(len(data))) print("first 40: {}".format(data[:40])) the_url = os.environ['BASE_DOMAIN'] + "/binary" res = requests.post(url=the_url, data=data, headers={'Content-Type': 'application/octet-stream'})
Discussions

python - How to get raw binary data from flask request, not unicode escaped - Stack Overflow
I am trying to get the raw binary data recieved by flask printed to the console. More on stackoverflow.com
🌐 stackoverflow.com
March 2, 2022
How to upload image file via binary file Not using form-data
Import StringIO, create stream then write to this stream from request.stream. Then call yourstream.getValue() to get binary data and save it. Maybe I forgot how this methods called exactly, but main idea the same. More on reddit.com
🌐 r/flask
2
3
June 21, 2018
python - how to store binary file recieved by Flask into postgres - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
March 9, 2015
Uploading Images To Flask - How To Turn Image Files Into 'Binary Data' (Multipart/form-data)
Flask is a Python micro-framework for web development. More on reddit.com
🌐 r/flask
7
5
November 13, 2018
🌐
GitHub
gist.github.com › Miserlou › fcf0e9410364d98a853cb7ff42efd35a
Flask serving binary data example · GitHub
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")).
🌐
Pelicandd
blog.pelicandd.com › article › 80
Streaming input and output in Flask
@flask.route("", methods=["POST"]) def demo(): with open("/tmp/flask-stream-demo", "bw") as f: chunk_size = 4096 while True: chunk = flask.request.stream.read(chunk_size) if len(chunk) == 0: return f.write(chunk)
🌐
Alexsm
alexsm.com › flask-serve-images-on-the-fly
alexsm@home | Serve images on the fly in a binary form with Flask
May 1, 2021 - import flask import cv2 app = flask.Flask(__name__) def get_image_path(id): # ... build the full path return image_path @app.route('/image/<int:id>') def serve_image(id): # Get the full image path image_path = get_image_path(id) # Read the original image im = cv2.imread(fname, cv2.IMREAD_ANYCOLOR) # Resize the image (here make it 9 times smaller) new_shape = (im.shape[1] // 3, im.shape[0] // 3) im_smaller = cv2.resize(im, new_shape) # Encode the resized image to PNG _, im_bytes_np = cv2.imencode('.png', im_smaller) # Constuct raw bytes string bytes_str = im_bytes_np.tobytes() # Create response given the bytes response = flask.make_response(bytes_str) response.headers.set('Content-Type', 'image/png') return response
Find elsewhere
🌐
MojoAuth
mojoauth.com › serialize-and-deserialize › serialize-and-deserialize-binary-with-flask
Serialize and Deserialize Binary with Flask | Serialize & Deserialize Data Across Languages
Ensure your server consistently sends valid pickle data for smooth client-side deserialization. You can accept raw binary data directly in a POST request body within your Flask routes.
🌐
GitHub
gist.github.com › yoavram › 4351498
Example of uploading binary files programmatically in python, including both client and server code. Client implemented with the requests library and the server is implemented with the flask library. · GitHub
Example of uploading binary files programmatically in python, including both client and server code. Client implemented with the requests library and the server is implemented with the flask libra...
🌐
Compile7
compile7.org › serialize-and-deserialize › how-to-serialize-and-deserialize-binary-in-flask
How to Serialize and Deserialize Binary in Flask - Compile7
November 26, 2025 - You must specify the mimetype to inform the client about the data type. Additionally, setting the Content-Disposition header is vital if you want the client to treat the response as a downloadable file.
🌐
jdhao's digital space
jdhao.github.io › 2020 › 04 › 12 › build_webapi_with_flask_s2
Build Web API with Flask -- Post and Receive Image · jdhao's digital space
June 17, 2020 - You can directly post binary image to the server using the files parameter of requests.post(): url = 'http://127.0.0.1:5000/im_size' my_img = {'image': open('test.jpg', 'rb')} r = requests.post(url, files=my_img) # convert server response into JSON format. print(r.json()) In the above code, ...
🌐
Stack Overflow
stackoverflow.com › questions › 48116820 › writing-python-request-using-post-to-flask-server-as-binary-file
writing python request using "post" to flask server as binary file - Stack Overflow
January 5, 2018 - # test 1 foo = request.get_data() #TEST 2 foo = request.files #test 3 foo = request.stream.read() f = request.files['file'] fname = f.filename #Using Hexa for the binary name and prefix bin fname_hexa = fname.encode("hex") bin_file_name="%s_%s"%("bin",fname_hexa) with open(bin_file_name, 'wb') as output_file_bin: output_file_bin.write(**foo**) output_file_bin.close() But I get an empty file whatever the method used to right "foo" : get_data() and stream.read() If I use only "files", I got an error message from my flask server :
🌐
GitHub
github.com › miguelgrinberg › Flask-SocketIO › issues › 195
Binary data support · Issue #195 · miguelgrinberg/Flask-SocketIO
January 5, 2016 - I've only seen string and json support mentioned. While sending the blob does get me the placeholder for the blob in the resulting json on the server, I don't know if the binary data is received/processed.
Author   chipsenkbeil
🌐
LinuxTut
linuxtut.com › en › bd6c5c486b31e87db44b
POST the image with json and receive it with flask
March 5, 2020 - In that case, the binary data may be converted into text data once according to a certain rule, transmitted, and then converted into the original binary data at the receiving destination. One of the rules for converting binary data to text data is base64.
🌐
Programmersought
programmersought.com › article › 6134597725
Flask uses a binary stream to store images and read them to the database. - Programmer Sought
s is the path of the file 100000 is the amount stored in the database at one time The returned result format is the amount of file data, the amount successfully stored in the database... The function realization is very simple: the arbitrary video is decomposed into frames, the frames are subjected to inter-frame difference operation, and finally the binary images obtained by the diff... ... OpenStack ---- Keystone component deployment! ! ! React mobile judging the current device is iOS or Android ... The difference between # and. in css ... Related Tags flaskpythonajaxhtmlBinaryimageVerification codedatabaseDevelopment languageComputer Vision