Q:

Google App Engine Python error : "Blob() argument should be str instance, not unicode "

I have been trying to save images on GAE data store but i get the following error:
"Blob() argument should be str instance, not unicode". 
Any ideas how to get past this ? 
The way i read and (trying to) write the images is :
...
avatar_data = self.request.get('pic_input') # pic_input is the name of the form
artist.picture = db.Blob(avatar_data) # artist is an entity type that has a picture field of type db.Blob()
...

I tried also to wrap avatar_data inside str() which actually got the string to be saved in the data store but my file didn't display !!!
Thank you in advance ! 

A:

What are you trying to do?
The response from pic_input is a unicode string, but you are trying to store it as if it were just binary bits. Are these binary bits or a string?
If they are binary bits, they should not have been encoded to unicode in the first place.
If it is a string, then you shouldn't store it in a Blob but a Text or String.
The reason the form upload is sending an encoded string (unicode) is because, you did not use the proper enctype in the form.
<form method = "post" enctype="multipart/form-data" >

Should solve this and your code will just work fine.

A:

Encode it!
avatar_data.encode('utf-8')

Replace utf-8 with the encoding you want, e.g., 'ascii'.

