The objects in request.files are FileStorage objects. They have the same methods as normal file objects in python.
So, to get the contents of the file as binary, just do this:
data = request.files['file'].read()
Then pass that parameter into the INSERT.
writing python request using "post" to flask server as binary file - Stack Overflow
How to upload image file via binary file Not using form-data
python - Flask to return image stored in database - Stack Overflow
python - Posting binary file and name in the same request in AngularJS and Flask - Stack Overflow
Hi, I am trying to upload image files using postman binary file. I read `request.stream.read()` but how can i save this file on specific folder.
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.
Just wanted to confirm that dav1d's second suggestion is correct - I tested this (where obj.logo is a mongoengine ImageField), works fine for me:
import io
from flask import current_app as app
from flask import send_file
from myproject import Obj
@app.route('/logo.png')
def logo():
"""Serves the logo image."""
obj = Obj.objects.get(title='Logo')
return send_file(
io.BytesIO(obj.logo.read()),
download_name='logo.png',
mimetype='image/png'
)
Easier than manually creating a Response object and settings its headers.
Prior to Flask 2.0, download_name was called attachment_filename.
Hi there,
I am trying to display an image that is sent from a flask backend using Flask's 'send_file' function. I believe send_file sends the image back as binary data. I am trying to use this response to create a new blob and display it on the screen for the user.
My react code:
let matrixData = response; //The response from flask's send_file
console.log(response);
let matrixBlob = new Blob([matrixData.data], {type:"image/jpg"});
console.log(matrixBlob);
let fileReader = new FileReader();
fileReader.readAsDataURL(matrixBlob);
fileReader.onload = () => {
let result = fileReader.result; console.log(result);
this.setState({ confusionMatrix: result
});
}The response data looks like this:
{data: "����JFIFdd��C…��↵(��↵(��↵(��↵(��↵(��↵(��↵(��↵(��↵(��↵(��↵(��?��", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data: "����JFIFdd��C"
headers: {last-modified: "Sun, 28 Apr 2019 20:34:17 GMT", content-type: "blob", cache-control: "public, max-age=43200", expires: "Mon, 29 Apr 2019 08:34:17 GMT"}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"
__proto__: Objectmy result from fileReader looks like this(truncated for posting purposes):
data:image/jpg;base64,77+977+977+977+9ABBKRklGAAEBAQBkAGQAAO+/ve+/vQBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgrvv73vv70AQwECAgICAgIFAwMFCgcGBwoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoK77+977+9ABEIAe+/vQLvv70DASIAAhEBAxEB77+977+9AB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC++/ve+/vQDvv70QAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQy77+977+977+9CCNC77+977+9FVLvv73vv70kM2Jy77+9CQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eu+/
Just in case, the flask code that sends the file:
return send_file('confusion_matrix.jpg', mimetype='image/jpg')When I look at the preview of the response in my Developer tools, I can see the image, but I guess I'm not converting it in a way that React expects?
EDIT: Code formatting