Skip to content Skip to sidebar Skip to footer

Saving Uploaded Base64 Data Gives Typeerror: A Bytes-like Object Is Required, Not 'str'

I am using a JavaScript library to upload an image. It places the image data in a form field that is a JSON encoded object with the file name and base64 encoded data. When I try to

Solution 1:

StringIO represents a string, not bytes, you want BytesIO. Base64 encoded data is not the actual image bytes, it needs to be decoded with b64decode.

file_data = io.BytesIO(b64decode(image_base))

Solution 2:

You need to encode either app.config['UPLOAD_FOLDER'] or , filename to be a byte-like object. Try the .encode('utf-8') method.

Post a Comment for "Saving Uploaded Base64 Data Gives Typeerror: A Bytes-like Object Is Required, Not 'str'"