CherryPy / cherrypy / tutorial /

"""Tutorial: File upload"""

import os
localDir = os.path.dirname(__file__)
absDir = os.path.join(os.getcwd(), localDir)

import cherrypy
from cherrypy.lib import cptools

class FileDemo(object):
    def index(self):
        return """
            <form action="upload" method="post" enctype="multipart/form-data">
            filename: <input type="file" name="myFile" /><br />
            <input type="submit" />
        """ = True
    def upload(self, myFile):
        out = """<html>
            myFile length: %s<br />
            myFile filename: %s<br />
            myFile mime-type: %s
        # Although this just counts the file length, it demonstrates
        # how to read large files in chunks instead of all at once.
        # CherryPy uses Python's cgi module to read the uploaded file
        # into a temporary file; reads from that.
        size = 0
        while True:
            data =
            if not data:
            size += len(data)
        return out % (size, myFile.filename, myFile.type) = True
    def download(self):
        path = os.path.join(absDir, "ReturnVsYield.pdf")
        return cptools.serveFile(path, "application/x-download",
                                 "attachment", os.path.basename(path)) = True

cherrypy.root = FileDemo()

if __name__ == '__main__':
    # Use the configuration file tutorial.conf.
    cherrypy.config.update(file = 'tutorial.conf')
    # Start the CherryPy server.