将图片从Mobile上传到Django服务器的最佳方法 [英] Best way to upload image from Mobile to Django server

查看:251
本文介绍了将图片从Mobile上传到Django服务器的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Titanmium中创建了一个移动应用程序.在该应用程序中,用户在移动设备中拍照,我需要将移动设备中的图像上传到django服务器.

任何人都可以指导我上载图片并将其保存在服务器中的最佳方法

这些方法可能是纯django或使用好吃的饼.任何方法都会有所帮助.

也是实现这一目标的最佳技术.

解决方案

(至少)有两种方法可以使用Django/Deliciouspie处理文件上传:

1/如我的评论所述:

您可以在此问题上利用Tastypie的功能. Django-tastypie:关于在POST中上传文件的示例吗?

2/您可以使用Django方式:

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

一个简单的示例(使用视图):

@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

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.

解决方案

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

这篇关于将图片从Mobile上传到Django服务器的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆