Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created September 17, 2012 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cjgiridhar/3735543 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3735543 to your computer and use it in GitHub Desktop.
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