I'm looking for a way to implement client side file (image) upload from jquery to Django-Tastypie.
So far server side seems correct testing with CURL:
I found this post helpfull Django-tastypie: Any example on file upload in POST?
EDIT : This is what i did with curl ->
in api.py :
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
data = request.POST.copy()
return super(MultipartResource, self).deserialize(request, data, format)
class FooResource(MultipartResource, ModelResource):
img = fields.FileField(attribute="img", null=True, blank=True)
queryset = Foo.objects.all()
in models.py :
img = models.ImageField(upload_to="images", null=True, blank=True)
body = models.CharField(max_length=255)
curl -v -F "body=test" -F "img=@my_picture.png" http://localhost:8000/api/v1/foo/
If you mean using
$.post() then it won't work because Ajax doesn't support file uploads. See e.g. this SO post.
There are some workarounds though - e.g. uploading via form in hidden iframe or flash-based solutions - they are discussed in the SO post mentioned above. None of them are perfect though, so you will have to choose the one which best serves your use case.