Package cherrypy :: Package tutorial :: Module tut09_files
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.tutorial.tut09_files

  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.static.serve_file for that; it takes four 
 28  arguments: 
 29   
 30  serve_file(path, content_type=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" (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 
 39  popular browsers. 
 40   
 41  """ 
 42   
 43  import os 
 44  localDir = os.path.dirname(__file__) 
 45  absDir = os.path.join(os.getcwd(), localDir) 
 46   
 47  import cherrypy 
 48  from cherrypy.lib import static 
 49   
 50   
51 -class FileDemo(object):
52
53 - def index(self):
54 return """ 55 <html><body> 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" /> 60 </form> 61 <h2>Download a file</h2> 62 <a href='download'>This one</a> 63 </body></html> 64 """
65 index.exposed = True 66
67 - def upload(self, myFile):
68 out = """<html> 69 <body> 70 myFile length: %s<br /> 71 myFile filename: %s<br /> 72 myFile mime-type: %s 73 </body> 74 </html>""" 75 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. 80 size = 0 81 while True: 82 data = myFile.file.read(8192) 83 if not data: 84 break 85 size += len(data) 86 87 return out % (size, myFile.filename, myFile.content_type)
88 upload.exposed = True 89
90 - def download(self):
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
95 96 97 import os.path 98 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 99 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) 105 else: 106 # This branch is for the test suite; you can ignore it. 107 cherrypy.tree.mount(FileDemo(), config=tutconf) 108