Skip to content

Instantly share code, notes, and snippets.

Forked from klipstein/
Created Dec 14, 2011
What would you like to do?
Base64 file handling for django-tastypie
import base64
import os
from tastypie.fields import FileField
from django.core.files.uploadedfile import SimpleUploadedFile
class Base64FileField(FileField):
A django-tastypie field for handling file-uploads through raw post data.
It uses base64 for en-/decoding the contents of the file.
class MyResource(ModelResource):
file_field = Base64FileField("file_field")
class Meta:
queryset = ModelWithFileField.objects.all()
In the case of multipart for submission, it would also pass the filename.
By using a raw post data stream, we have to pass the filename within our
file_field structure:
file_field = {
"name": "myfile.png",
"file": "longbas64encodedstring",
"content_type": "image/png" # on hydrate optional
def dehydrate(self, bundle):
if not and hasattr(bundle.obj, self.instance_name):
file_field = getattr(bundle.obj, self.instance_name)
if file_field:
content_type, encoding = mimetypes.guess_type(
b64 = open(, "rb").read().encode("base64")
ret = {
"name": os.path.basename(,
"file": b64,
"content-type": content_type or "application/octet-stream"
return ret
return None
def hydrate(self, obj):
value = super(FileField, self).hydrate(obj)
if value:
value = SimpleUploadedFile(value["name"], base64.b64decode(value["file"]), getattr(value, "content_type", "application/octet-stream"))
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment