上传文件
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.render('/home/zz/index.html') class UploadHandler(tornado.web.RequestHandler): def post(self): if self.request.files: myfile = self.request.files['myfile'][0] fin = open("/home/zz/in.jpg","w") print "success to open file" fin.write(myfile["body"]) fin.close() application=tornado.web.Application([(r'/',MainHandler),(r'/upload', UploadHandler) ] ) if __name__=='__main__': application.listen(2033) tornado.ioloop.IOLoop.instance().start()
<html> <body> <form action="/upload" enctype="multipart/form-data" method="post"> <input name="myfile" type="file"> <input type="submit" value="Submit"> </form> </body></html>
其中python中UploadHandler中的“/upload”一定要出现在index.html中的表单的action属性中,必须一致。
action和methhod 都是对当前form的提交进行设置
action="提交到地址以及相关参数"
method="设置提交方式"