Search code examples
djangotitanium-mobileappceleratortastypieappcelerator-mobile

Best way to upload image from Mobile to Django server


I am created a Mobile application(in Titanmium).where user take pictures in mobile and i need To upload the image from mobile to django server .I am using tastypie for my Api

can any one guide me the best way to upload and save the image in server

the methods may be in pure django or using tastypie .Anything will be helpful.

and also best technique to acheieve this.


Solution

  • There are (at least) two ways to handle file upload with Django / Tastypie :

    1/ As stated in my comment :

    you can make use of Tastypie's features regarding the matter. Django-tastypie: Any example on file upload in POST?

    2/ You can go the Django way :

    https://docs.djangoproject.com/en/1.6/topics/http/file-uploads/

    A quick example (using a view) :

    @csrf_exempt
    def handle_uploads(request):
        if request.method == 'POST':
            uploaded_file = request.FILES['file']
            file_name = uploaded_file.name
            # Write content of the file chunk by chunk in a local file (destination)
            with open('path/to/destination_dir/' + file_name, 'wb+') as destination:
                for chunk in uploaded_file.chunks():
                    destination.write(chunk)
    
        response = HttpResponse('OK')
        return response