Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.1/cherrypy/tutorial/tut09_files.py

Revision 727 (checked in by fumanchu, 3 years ago)

Tutorial fixes, plus a bug in _cputil.getErrorPage.

Line 
1 """
2
3 Tutorial: File upload and download
4
5 Uploads
6 -------
7
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.
14
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.
17
18
19 Downloads
20 ---------
21
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.cptools.serveFile for that; it takes four
28 arguments:
29
30 serveFile(path, contentType=None, disposition=None, name=None)
31
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" ("application/x-download" works in most cases).
35
36 """
37
38 import os
39 localDir = os.path.dirname(__file__)
40 absDir = os.path.join(os.getcwd(), localDir)
41
42 import cherrypy
43 from cherrypy.lib import cptools
44
45
46 class FileDemo(object):
47    
48     def index(self):
49         return """
50         <html><body>
51             <form action="upload" method="post" enctype="multipart/form-data">
52             filename: <input type="file" name="myFile" /><br />
53             <input type="submit" />
54             </form>
55         </body></html>
56         """
57     index.exposed = True
58    
59     def upload(self, myFile):
60         out = """<html>
61         <body>
62             myFile length: %s<br />
63             myFile filename: %s<br />
64             myFile mime-type: %s
65         </body>
66         </html>"""
67        
68         # Although this just counts the file length, it demonstrates
69         # how to read large files in chunks instead of all at once.
70         # CherryPy uses Python's cgi module to read the uploaded file
71         # into a temporary file; myFile.file.read reads from that.
72         size = 0
73         while True:
74             data = myFile.file.read(8192)
75             if not data:
76                 break
77             size += len(data)
78        
79         return out % (size, myFile.filename, myFile.type)
80     upload.exposed = True
81    
82     def download(self):
83         path = os.path.join(absDir, "pdf_file.pdf")
84         return cptools.serveFile(path, "application/x-download",
85                                  "attachment", os.path.basename(path))
86     download.exposed = True
87
88
89 cherrypy.root = FileDemo()
90
91 if __name__ == '__main__':
92     # Use the configuration file tutorial.conf.
93     cherrypy.config.update(file = 'tutorial.conf')
94     # Start the CherryPy server.
95     cherrypy.server.start()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets