2012-10-09 77 views
7

如何在使用put請求時訪問Tornado上傳的文件?用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. 

回答

7
@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頁面。

+0

'bytes'只接受一個參數(python 2.7),有沒有錯字? – daniel

10

self.request.files應該沒問題。這是一個example

相關問題