Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tornado - File Uploads
import tornado
import tornado.ioloop
import tornado.web
import os, uuid
__UPLOADS__ = "uploads/"
class Userform(tornado.web.RequestHandler):
def get(self):
self.render("fileuploadform.html")
class Upload(tornado.web.RequestHandler):
def post(self):
fileinfo = self.request.files['filearg'][0]
print "fileinfo is", fileinfo
fname = fileinfo['filename']
extn = os.path.splitext(fname)[1]
cname = str(uuid.uuid4()) + extn
fh = open(__UPLOADS__ + cname, 'w')
fh.write(fileinfo['body'])
self.finish(cname + " is uploaded!! Check %s folder" %__UPLOADS__)
application = tornado.web.Application([
(r"/", Userform),
(r"/upload", Upload),
], debug=True)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.