Skip to content

Instantly share code, notes, and snippets.

@hivefans
Forked from cjgiridhar/tornadofileupload.py
Last active March 17, 2020 02:03
Show Gist options
  • Save hivefans/8121344 to your computer and use it in GitHub Desktop.
Save hivefans/8121344 to your computer and use it in GitHub Desktop.
|-|{"files":{"tornadofileupload.py":{"env":"plain"}},"tag":"bigdata"}
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