Processing files in Django when sending an image from a service call

I am using PyAMF to migrate a dynamically created large image from Flex to Django. On the Django side, I get encodedb64 data as a parameter:

My Item model as an image field. I have problems saving data as a Django file field.

def save_item(request, uname, data): """ Save a new item """ item = Item() img = cStringIO.StringIO() img.write(base64.b64decode(data)) myFile = File(img) item.preview.save('fakename.jpg', myFile, save=False) 

This will not work, because my FileIO object from StringIO skips some properties such as mode, name, etc.

I also think that using StringIO will completely load the image data in memory, which is bad, so I can just abandon AMF for this particular case and use POST.

What do you think?

+7
python flex django pyamf
source share
1 answer

In django.core.files.base you can find the ContentFile class. This class extends the base class of Django File , so you don't need StringIO (which the ContentFile uses internally). The modified save method is as follows:

 from django.core.files.base import ContentFile def save_item(request, uname, data): item = Item() myFile = ContentFile(base64.b64decode(data)) item.preview.save('fakename.jpg', myFile, save=False) 
+9
source share

All Articles