问题描述
我创建了一个移动应用程序(在 Titanmium 中).用户在其中使用移动设备拍照,我需要将图像从移动设备上传到 django 服务器.我的 Api 使用的是tastypie
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
方法可能是在纯 django 中或使用tastypie.任何事情都会有帮助.
the methods may be in pure django or using tastypie .Anything will be helpful.
也是实现这一目标的最佳技术.
and also best technique to acheieve this.
推荐答案
使用 Django/Tastypie 有(至少)两种方式来处理文件上传:
There are (at least) two ways to handle file upload with Django / Tastypie :
1/如我的评论所述:
您可以利用 Tastypie 的相关功能.Django-tastypie:关于 POST 文件上传的任何示例?一个>
you can make use of Tastypie's features regarding the matter. Django-tastypie: Any example on file upload in 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
这篇关于将图像从 Mobile 上传到 Django 服务器的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!