cherrypy.tutorial.tut09_files module

Tutorial: File upload and download


When a client uploads a file to a CherryPy application, it’s placed on disk immediately. CherryPy will pass it to your exposed method as an argument (see “myFile” below); that arg will have a “file” attribute, which is a handle to the temporary uploaded file. If you wish to permanently save the file, you need to read() from myFile.file and write() somewhere else.

Note the use of ‘enctype=”multipart/form-data”’ and ‘input type=”file”’ in the HTML which the client uses to upload the file.


If you wish to send a file to the client, you have two options: First, you can simply return a file-like object from your page handler. CherryPy will read the file and serve it as the content (HTTP body) of the response. However, that doesn’t tell the client that the response is a file to be saved, rather than displayed. Use cherrypy.lib.static.serve_file for that; it takes four arguments:

serve_file(path, content_type=None, disposition=None, name=None)

Set “name” to the filename that you expect clients to use when they save your file. Note that the “name” argument is ignored if you don’t also provide a “disposition” (usually “attachement”). You can manually set “content_type”, but be aware that if you also use the encoding tool, it may choke if the file extension is not recognized as belonging to a known Content-Type. Setting the content_type to “application/x-download” works in most cases, and should prompt the user with an Open/Save dialog in popular browsers.

class cherrypy.tutorial.tut09_files.FileDemo[source]

Bases: object