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

Ticket #547: serve_file.patch

  • tutorial/tut09_files.py

    old new  
    2727Use cherrypy.lib.static.serve_file for that; it takes four 
    2828arguments: 
    2929 
    30 serve_file(path, contentType=None, disposition=None, name=None) 
     30serve_file(path, content_type=None, disposition=None, name=None) 
    3131 
    3232Set "name" to the filename that you expect clients to use when they save 
    3333your file. Note that the "name" argument is ignored if you don't also 
    3434provide a "disposition" (usually "attachement"). You can manually set 
    35 "contentType", but be aware that if you also use the encoding tool, it 
     35"content_type", but be aware that if you also use the encoding tool, it 
    3636may choke if the file extension is not recognized as belonging to a known 
    37 Content-Type. Setting the contentType to "application/x-download" works 
     37Content-Type. Setting the content_type to "application/x-download" works 
    3838in most cases, and should prompt the user with an Open/Save dialog in 
    3939popular browsers. 
    4040 
  • lib/static.py

    old new  
    1313from cherrypy.lib import cptools, http 
    1414 
    1515 
    16 def serve_file(path, contentType=None, disposition=None, name=None): 
     16def serve_file(path, content_type=None, disposition=None, name=None): 
    1717    """Set status, headers, and body in order to serve the given file. 
    1818     
    19     The Content-Type header will be set to the contentType arg, if provided. 
     19    The Content-Type header will be set to the content_type arg, if provided. 
    2020    If not provided, the Content-Type will be guessed by its extension. 
    2121     
    2222    If disposition is not None, the Content-Disposition header will be set 
     
    5151    response.headers['Last-Modified'] = http.HTTPDate(stat.st_mtime) 
    5252    cptools.validate_since() 
    5353     
    54     if contentType is None: 
     54    if content_type is None: 
    5555        # Set content-type based on filename extension 
    5656        ext = "" 
    5757        i = path.rfind('.') 
    5858        if i != -1: 
    5959            ext = path[i:].lower() 
    60         contentType = mimetypes.types_map.get(ext, "text/plain") 
    61     response.headers['Content-Type'] = contentType 
     60        content_type = mimetypes.types_map.get(ext, "text/plain") 
     61    response.headers['Content-Type'] = content_type 
    6262     
    6363    if disposition is not None: 
    6464        if name is None: 
     
    101101                def fileRanges(): 
    102102                    for start, stop in r: 
    103103                        yield "--" + boundary 
    104                         yield "\nContent-type: %s" % contentType 
     104                        yield "\nContent-type: %s" % content_type 
    105105                        yield ("\nContent-range: bytes %s-%s/%s\n\n" 
    106106                               % (start, stop - 1, c_len)) 
    107107                        bodyfile.seek(start) 
     
    132132        if content_types: 
    133133            r, ext = os.path.splitext(filename) 
    134134            content_type = content_types.get(ext[1:], None) 
    135         serve_file(filename, contentType=content_type) 
     135        serve_file(filename, content_type=content_type) 
    136136        return True 
    137137    except cherrypy.NotFound: 
    138138        # If we didn't find the static file, continue handling the 

Hosted by WebFaction

Log in as guest/cpguest to create tickets