Quite often we’re in need of providing file upload mechanism on our website. Be it logs management or user profile management, support for file upload is a must. This blog describes how uploads be achieved with Tornado web server.

Example code:

In this code snippet;

  • When user browses to http://localhost:8888/, he is presented with file upload form (code below)
  • On browsing and selecting the appropriate file, the user clicks on upload
  • The file gets uploaded and the user gets a message with filename & the uploaded location

In the upload form, its important to note the usage of below tags for file uploads:

  • enctype=”multipart/form-data”
  • input type=”file

As a side note, if you print fileinfo variable, you would observe a dictionary with contents and meta-data of file being uploaded

fileinfo is {'body': 'This is a file upload test for Tornado!!\n', 'content_type': u'application/octet-stream', 'filename': u'fileuploadtest'}