简体   繁体   English

django-piston模块可以创建上传图片restful webservice

[英]Can django-piston module create upload image restful webservice

我试图用一台可以上传图像的宁静服务器,通过使用django-piston我可以放置,获取,发布信息,但不知道如何上传图像。

pretty much. 差不多。

technically it's just a http post. 从技术上讲,它只是一个http帖子。

On the one hand, yes. 一方面,是的。 If you have the image data, it's possible to send it via post to a handler that knows how to handle it; 如果你有图像数据,可以通过post发送给知道如何处理它的处理程序; if you do it right, it should, theoretically, be available in request.FILES to your handler. 如果你做对了,从理论上讲,它应该在request.FILES中提供给你的处理程序。 Simple HTTP. 简单的HTTP。

On the other hand, no. 另一方面,没有。 In order to do an AJAX upload like this, you would have to somehow get the image data without the user actually submitting a form. 为了像这样进行AJAX上传,您必须以某种方式获取图像数据,而无需用户实际提交表单。 This is why "ajax upload forms" are so difficult to implement, and usually use tricks like hidden iframes to do their stuff. 这就是“ajax上传表单”难以实现的原因,并且通常使用隐藏的iframe等技巧来完成他们的工作。

To the best of my knowledge, only Firefox and its gecko kin allow this kind of access to a file field's binary content, via the File object's getAsBinary() method. 据我所知,只有Firefox及其gecko kin允许通过File对象的 getAsBinary()方法访问文件字段的二进制内容。

You can certainly do the POST. 你当然可以做POST。 The file(s) will be available in the request.FILES (piston won't get in the way of this). 文件将在request.FILES中可用(活塞不会妨碍这一点)。

In order to do the PUT, we'll have to make some changes to piston to support the x-method-override header. 为了执行PUT,我们必须对活塞进行一些更改以支持x-method-override标头。 That's what I do to allow PUT and DEL from flash. 这就是我从闪存中允许PUT和DEL的方法。 ( Don't forget to add the header when you do the POST to make it get interpreted as a PUT ) (执行POST时不要忘记添加标题以使其被解释为PUT)

Here's some example middleware: 这是一些示例中间件:

class x_http_methodoverride_middleware():
    def process_request(self, request):
        if 'HTTP_X_HTTP_METHODOVERRIDE' in request.META:
            newMethod = request.META['HTTP_X_HTTP_METHODOVERRIDE']
            if 'PUT' == newMethod.upper():
                request.method = 'PUT'
                request.META['REQUEST_METHOD'] = 'PUT'
                request.PUT = request.POST                
            if 'DELETE' == newMethod.upper() or 'DEL' == newMethod.upper():
                request.method = 'DELETE'
                request.META['REQUEST_METHOD'] = 'DELETE'
                request.DELETE = request.POST

( the code is from an open piston ticket here http://bitbucket.org/jespern/django-piston/issue/83/use-x-http-method-override-to-override-put ) (代码来自一张开放式活塞门票http://bitbucket.org/jespern/django-piston/issue/83/use-x-http-method-override-to-override-put

You can find two answers here: http://groups.google.com/group/django-piston/browse_thread/thread/6f3f964b8b3ccf72/bd1658121bb1874c?show_docid=bd1658121bb1874c&pli=1 你可以在这里找到两个答案: http//groups.google.com/group/django-piston/browse_thread/thread/6f3f964b8b3ccf72/bd1658121bb1874c?show_docid=bd1658121bb1874c&pli=1

One way is to use request.FILES to get the filename, and then to save the image: 一种方法是使用request.FILES获取文件名,然后保存图像:

def create(self, request, nickname): 
    name = request.FILES["image"].name 
    image = PIL.Image.open(request.FILES["image"]) 
    image.save(SOME_PATH+name) 
    return rc.ALL_OK 

The second suggestion is to define an Image model and an ImageForm form, and use those: 第二个建议是定义一个Image模型和一个ImageForm表单,并使用它们:

def create(self, request, nickname):
    form = ImageForm(request.POST, request.FILES)
    if form.is_valid():
        Image.objects.create(image=form.cleaned_data['image'])
        return rc.ALL_OK
    return rc.BAD_REQUEST

WARNING: I haven't tested either of these methods! 警告:我没有测试过这些方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM