关于tornado文件上传与存储

#!/usr/bin/python
#-*- encoding:utf-8 -*-
import tornado.ioloop
import tornado.web
import shutil
import os

class UploadFileHandler(tornado.web.RequestHandler):
    def get(self):
        self.write('''

  Upload File
  
    

''') def post(self): upload_path=os.path.join(os.path.dirname(__file__),'files') #文件的暂存路径(在py文件同目录下创一个files文件夹来存储) file_metas=self.request.files['file'] #提取表单中‘name’为‘file’的文件元数据(一个文件元包括:[{'body':' XXX', 'content_type':'XXXX', 'filename':'XXXX'}]) for meta in file_metas: filename=meta['filename'] filepath=os.path.join(upload_path,filename) with open(filepath,'wb') as up: #有些文件需要已二进制的形式存储,实际中可以更改(注意学习with ... as 关键词的用法) up.write(meta['body']) self.write('finished!') app=tornado.web.Application([ (r'/file',UploadFileHandler), ]) if __name__ == '__main__': app.listen(3000) tornado.ioloop.IOLoop.instance().start()


 
 

你可能感兴趣的:(web)