我创建了一个移动应用程序(在Titanmium中。)用户在移动设备上拍照,我需要将图像从手机上传到django服务器。我正在使用tastypie为我的Api
任何人都可以指导我在服务器上传和保存图像的最佳方式
方法可能是纯粹的django或使用tastypie。任何东西都会有所帮助。
也是最好的技巧。
答案 0 :(得分:1)
使用Django / Tastypie(至少)有两种处理文件上传的方法:
1 /正如我的评论所述:
你可以利用Tastypie的相关功能。 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