使用Tornado上传文件

时间:2012-10-09 10:14:42

标签: python python-3.x tornado

使用put请求时,如何在Tornado中访问上传的文件?

@require_basic_auth
class UploadFile(tornado.web.RequestHandler):
    def put(self, params):
        path = calculate_path(params)
        # TODO: create an empty binary file at path and then copy 
        # the request input stream to it.

2 个答案:

答案 0 :(得分:10)

self.request.files应该没问题。这是一个example

答案 1 :(得分:6)

@require_basic_auth
class UploadFile(tornado.web.RequestHandler):
    def put(self, params):
        path = calculate_path(params)
        with open(path, 'wb') as out:
            body = self.request.get_argument('data')
            out.write(bytes(body, 'utf8'))        

......就是我所需要的。

在某些ActiveState页面上找到。