Stack Overflow Asked by a-clmnt on December 28, 2020
I’m trying to upload a base64 image (encoded in Python) to my webapp using Flask.
I get the encoded image but get an error when modifying the src of the image element.
Ajax:
$.ajax({
dataType: "json",
type: 'POST',
url: getWebAppBackendUrl("return_cam"),
data: {
image_bytes: img_b64
},
success: function (filter_bytes) {
let data = filter_bytes.heatmap
document.getElementById("cam").src = "data:image/png;base64," + data;
}
});
server side:
@app.route('/return_cam', methods=['GET', 'POST'])
def return_cam():
img_b64 = request.form.get("image_bytes") #get POST data
api_result = client.run_function("returnCAM", image=img_b64)
response = api_result.get("response")
result_dict = json.loads(response)
return json.dumps(result_dict)
The ‘data’ variable looks like this when printed in the console:
b'ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj/ADj...'
and I get ERR_INVALID_URL.
I guess there is some processing to do on this data variable…
Thanks for the help!
Looks like you need to decode that value. Something like:
img_b64 = request.form.get("image_bytes")
img_b64 = img_b64.decode('utf-8')
#...
Not sure if client.run_function
will appreciate this, as it's not clear from your question what that method does. Perhaps you could put the above mod within that function if you wrote it yourself, so that the correctly decoded value ends up within response
as expected.
Answered by v25 on December 28, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP