3 Tutorial: File upload and download
8 When a client uploads a file to a CherryPy application, it's placed
9 on disk immediately. CherryPy will pass it to your exposed method
10 as an argument (see "myFile" below); that arg will have a "file"
11 attribute, which is a handle to the temporary uploaded file.
12 If you wish to permanently save the file, you need to read()
13 from myFile.file and write() somewhere else.
15 Note the use of 'enctype="multipart/form-data"' and 'input type="file"'
16 in the HTML which the client uses to upload the file.
22 If you wish to send a file to the client, you have two options:
23 First, you can simply return a file-like object from your page handler.
24 CherryPy will read the file and serve it as the content (HTTP body)
25 of the response. However, that doesn't tell the client that
26 the response is a file to be saved, rather than displayed.
27 Use cherrypy.lib.static.serve_file for that; it takes four
30 serve_file(path, content_type=None, disposition=None, name=None)
32 Set "name" to the filename that you expect clients to use when they save
33 your file. Note that the "name" argument is ignored if you don't also
34 provide a "disposition" (usually "attachement"). You can manually set
35 "content_type", but be aware that if you also use the encoding tool, it
36 may choke if the file extension is not recognized as belonging to a known
37 Content-Type. Setting the content_type to "application/x-download" works
38 in most cases, and should prompt the user with an Open/Save dialog in
44 localDir
= os
.path
.dirname(__file__
)
45 absDir
= os
.path
.join(os
.getcwd(), localDir
)
48 from cherrypy
.lib
import static
51 class FileDemo(object):
56 <h2>Upload a file</h2>
57 <form action="upload" method="post" enctype="multipart/form-data">
58 filename: <input type="file" name="myFile" /><br />
59 <input type="submit" />
61 <h2>Download a file</h2>
62 <a href='download'>This one</a>
67 def upload(self
, myFile
):
70 myFile length: %s<br />
71 myFile filename: %s<br />
76 # Although this just counts the file length, it demonstrates
77 # how to read large files in chunks instead of all at once.
78 # CherryPy reads the uploaded file into a temporary file;
79 # myFile.file.read reads from that.
82 data
= myFile
.file.read(8192)
87 return out
% (size
, myFile
.filename
, myFile
.content_type
)
91 path
= os
.path
.join(absDir
, "pdf_file.pdf")
92 return static
.serve_file(path
, "application/x-download",
93 "attachment", os
.path
.basename(path
))
94 download
.exposed
= True
98 tutconf
= os
.path
.join(os
.path
.dirname(__file__
), 'tutorial.conf')
100 if __name__
== '__main__':
101 # CherryPy always starts with app.root when trying to map request URIs
102 # to objects, so we need to mount a request handler root. A request
103 # to '/' will be mapped to HelloWorld().index().
104 cherrypy
.quickstart(FileDemo(), config
=tutconf
)
106 # This branch is for the test suite; you can ignore it.
107 cherrypy
.tree
.mount(FileDemo(), config
=tutconf
)