Base 64 Image fields

1,038 views
Skip to first unread message

Yigit Guler

unread,
Nov 25, 2013, 7:02:27 AM11/25/13
to django-res...@googlegroups.com
Hi everyone,

As far as i know, sending images as a multipart attachment is not the best approach in an API. Most of the REST API's accepts base64 encoded strings for image uploading. However for the GET requests, they are sending the URL of the image.

In Django Rest Framework, is there an easy way to accept base64 encoded image fields for POST and PUT requests? 

Thank you.

Tom Christie

unread,
Nov 26, 2013, 6:56:29 AM11/26/13
to django-res...@googlegroups.com
> As far as i know, sending images as a multipart attachment is not the best approach in an API. Most of the REST API's accepts base64 encoded strings for image uploading.

I think that's probably got as much to do with framework limitations as anything else.  Using multipart uploads is pretty sensible as it means the file upload can be properly handled by the parser and streamed to disk for large uploads.  With base64 strings embedded in JSON you'll always end up with the file in memory, which may not be desirable for large uploads.  Having said that, I don't think it's unreasonable and does at least keep the client requests limited to a single format.

For very large uploads I'd recommend raw file uploads using the `FileUploadParser` class, as that'll be the most efficient in terms of upload request size.

If you want base64 embedded in JSON you'd probably want to do something like a custom serializer ImageField that overrides `field_from_native` to decode the base64 string and save it as an `UploadedFile`, *or* use a standard ImageField, but write a custom parser where you can specify which fields should be treated as file uploads.

I know that's a little sparse on detail, but hopefully it's enough to get you started - would be interested to see any follow up to this.

Cheers,

  Tom

Yigit Guler

unread,
Nov 28, 2013, 5:14:21 AM11/28/13
to django-res...@googlegroups.com
Ok i see :) I will start writing a new field for base64 images. I'll inform you about the situation during the process.

Thank you.

26 Kasım 2013 Salı 13:56:29 UTC+2 tarihinde Tom Christie yazdı:

Yuri Prezument

unread,
Nov 29, 2013, 5:34:21 AM11/29/13
to django-res...@googlegroups.com
I used a base64 image field once, I can share the code if you're interested...

Yuri Prezument

unread,
Nov 29, 2013, 5:38:55 AM11/29/13
to django-res...@googlegroups.com

Yigit Guler

unread,
Nov 29, 2013, 5:47:55 AM11/29/13
to django-res...@googlegroups.com
Thank you! 



29 Kasım 2013 Cuma 12:38:55 UTC+2 tarihinde Yuri Prezument yazdı:

Yigit Guler

unread,
Dec 3, 2013, 7:50:04 AM12/3/13
to django-res...@googlegroups.com
Hi Yuri,

I analyzed your code. It works flawlessly. But in order to make this field compatible with our iphone app, i was obligated to make a field that works without the initial data  "data:image/jpeg;base64,".

So i read the file through imagehdr to determine the type.

Here is the full code:

http://pastebin.com/wNZYujeP

Just wanted to share. Maybe someone can use it someday! :)

Regards

Yuri Prezument

unread,
Dec 3, 2013, 8:20:27 AM12/3/13
to django-res...@googlegroups.com
Looks great...

Maybe it's worth making a package of it if it's useful to people?

Yigit Guler

unread,
Dec 4, 2013, 7:35:13 AM12/4/13
to django-res...@googlegroups.com
I made a pull request. Hope that it will be useful.

Regards.

3 Aralık 2013 Salı 15:20:27 UTC+2 tarihinde Yuri Prezument yazdı:

Tom Christie

unread,
Dec 4, 2013, 12:05:49 PM12/4/13
to django-res...@googlegroups.com
For anyone else interested the PR and associated discussion is here: https://github.com/tomchristie/django-rest-framework/pull/1268
Reply all
Reply to author
Forward
0 new messages