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

本文是小编为大家收集整理的关于从手机上传图片到Django服务器的最佳方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是创建的一个移动应用程序(在泰坦mium中).用户在移动设备中拍照,我需要将图像从移动设备上传到django服务器.我正在使用tastypie for my api

任何人都可以指导我上传并保存图像中的最佳方法

这些方法可能是在纯django中或使用tastypie.任何东西都会有所帮助.

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

推荐答案

使用Django/tastypie处理文件上传的两种方法:

1/如我的评论中所述:

您可以利用Tastypie的功能. django-tastypie:post中的任何示例?

2/您可以以Django方式走:

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

本文地址:https://www.itbaoku.cn/post/1938173.html

问题描述

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