Sanjay wrote:
> I need to store and download files from/to database. I am facing some
> problem for downloading:
>
> If I use serveFile (from cherrypy.lib.cptools import serveFile), I need
> to supply a path. That means, first I need to fetch the file from the
> database it to a temporary file in the webserver.
>
> If I use @expose(content_type="..."), I face two problems. First, I
> need to know the content_type (which I don't know how to have based on
> file extension - there is any helper method? However, it's sufficient
> for my users just to get the download dialog box, be it any kind of
> file.) Secondly, I don't know how to supply the filename, so that the
> downloaded file gets the proper name on the users' PC.
Here's a method similar to 2.2.1's serveFile which should do the trick.
In other words, it's hacked directly from serveFile ;) Feel free to
look deeper into serveFile to see how it works--it's pretty simple. You
may need to modify it (I don't know what attributes you have on files
in the DB), but the basics are here:
def serveDBFile(path, content, disposition=None, name=None):
"""Set headers in order to serve the given file.
The Content-Type will be guessed by the extension on "path".
If disposition is not None, the Content-Disposition header will be
set
to "<disposition>; filename=<name>". If name is None, it will be
set
to the basename of path. If disposition is None, no
Content-Disposition
header will be written.
"""
response = cherrypy.response
# Set content-type based on filename extension
ext = ""
i = path.rfind('.')
if i != -1:
ext = path[i:].lower()
contentType = mimetypes.types_map.get(ext, "text/plain")
response.headers['Content-Type'] = contentType
if disposition is not None:
if name is None:
name = os.path.basename(path)
cd = "%s; filename=%s" % (disposition, name)
response.headers["Content-Disposition"] = cd
response.headers['Content-Length'] = len(content)
Robert Brewer
System Architect
Amor Ministries
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---