[mod_python] binary file, part II: name of file?

Deron Meranda deron.meranda at gmail.com
Tue Jun 6 01:21:42 EDT 2006


On 6/5/06, Webb Sprague <webb.sprague at gmail.com> wrote:
> Is there anyway to specify programmatically a good default name for a
> binary file?  My URL is /blah/objectDump?id=5, and the default
> filename Windows gives for saving it is "objectDump"--I would like to
> generate the filename and have it be something like
> "forecast-object-5.txt'

Perhaps the best way is to send a Content-Disposition header
(in addition to your Content-Type header).  You'll usually send
one of these two variants:

  Content-Disposition: inline; filename=forecast-object-5.txt
  Content-Disposition: attachment; filename=forcast-object-5.txt

If the filename contains any HTTP special characters (called
"separators") then you'll need to quote the filename using
the HTTP quoted-string syntax.

The "inline" form will cause the browser to try to display the
file inline, if possible (e.g., displayed in the browser window).
The "attachment" form will cause the browser to open a
"save as" dialog, using the filename parameter as the
suggested filename.

The other (less desirable) way is to construct your URL
with extra unused path components at the end, such that
the last one is the filename.

You can also use some of the maps and functions in the
standard Python module "mimetypes" to help you construct
a proper filename (such as picking an extension given
a content-type).
-- 
Deron Meranda


More information about the Mod_python mailing list