'How to write file with data is Uint8Array?

I have some problems write file ( image ) to folder.

my script I get Uint8Array() from image

var arrayBuffer = event.target.result
var byteArray = new Uint8Array(arrayBuffer);

Then, I used "json.dumps", objData['imagedata'] is "var byteArray "

print(objData['imagedata'])
print("=========================================================================")
user_encode_data = json.dumps(objData['imagedata']).encode('utf-8')
print(user_encode_data)

enter image description here

Example image is print(user_encode_data) then I saved it in mysql with data type as longblob.

read_blob(1,"./MainArchives/images/data.png") 

def read_blob(author_id, filename):
   query = *my syntax query*
   ...
   photo = cursor.fetchone()[0] // "photo" is "user_encode_data" when I get back from mysql => as mentioned above *
   write_file(photo, filename) 

And Finally is function "write_file"

def write_file(data, filename):
   with open(filename, 'wb') as f:
       f.write(data)

And i get error as: ==> TypeError: a bytes-like object is required, not 'str'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source