您的位置:首页 > 其它

Tornado 上传文件及存储

2013-05-29 18:32 148 查看
http://blog.csdn.net/zhoucy163/article/details/8777839

上传文件

[python] view
plaincopy

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] view
plaincopy

<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="设置提交方式"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: