After some digging, I finally worked it out. In case anyone is
interested, here is the code for Django:

from django.core.files.uploadedfile import SimpleUploadedFile

def set_user_image(request, img_id):
  """
  set/update a user's image
  """
  if not request.user.is_authenticated():
    return HttpResponse(status=403)

  # get the profile for the logged on user
  profile = get_object_or_404(GenericProfile, user = request.user)

  # instantiate an uploaded file to be passed to the model
  upfile = SimpleUploadedFile("%s.jpg" % img_id,
                              request._raw_post_data,
                              "image/jpeg")

  # update the user profile
  profile.image.save("%s.jpg" % img_id, upfile, True)

  return HttpResponse()

This code lacks important functionality though, e.g. check if the file
is a valid JPEG image, check if img_id is valid, etc.

So basically all you have to do is create a new SimpleUploadFile
object and initialize it with the raw POST data. Of course the client
has to use the correct format; in Flex, I did something like this:

  var req:URLRequest = new URLRequest(baseURL + "setimage/" + _field);
  req.method = "POST";
  req.data = jpData; /* this is the ByteArray containing JPEG data */
  req.contentType = "image/jpeg";

  var loader:URLLoader = new URLLoader();
  loader.addEventListener(Event.COMPLETE, uploadOK);
  loader.addEventListener(IOErrorEvent.IO_ERROR, uploadFault);
  loader.load(req);

To all you Django gurus out there: is this a sensible approach?

Thanks and happy holidays!

Markus

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to