durusmail: quixote-users: How to have a form submit go to file save dialog
How to have a form submit go to file save dialog
2003-12-13
2003-12-14
How to have a form submit go to file save dialog
Jason E. Sibre
2003-12-14
> 'name' here is what the user enters into a form that asks him what
> to name the file.
>
> ###
>         if request.form:
>             name = request.form['name']
>
>             # construct the file
>             file = self.construct_sisterXML(request, 1)
>
>             # force a download to a filename
>             request.response.set_header('Content-Disposition',
>                                         'attachment; filename=%s'
> % (name,))
>             request.response.set_header('Content-Length', len(file))
>
>             return file
> ###
>
> I think the magic information is in the Content-Disposition header.
>
> cheers,
> --titus


I haven't done much of this with Quixote, but I've done quite a bit of this
header juggling with other technologies (using the same technique Titus
illustrates nicely above), and just thought I'd mention a thing or two:

  One - Certain versions of MSIE 5.0 (one or two of the service pack levels
for it) had a bug which prevented this from working correctly, even when
everything was done correctly by the developer.  If you're targeting that
browser, and having troubles, don't just assume you're doing something
wrong;  try another browser first.

  Two - Quixote gives you a GREAT way around the problem...  Due to
_q_lookup, you can actually do something like
"http://myserver.com/my-quixote-app/download/23/filename.ext".  In this
example, I'd use the 23 indicate an ID number to look some filedata up in a
database and retrieve it (for example), and then consume the "filename.ext"
for no useful purpose (on the server end) and then send the result back
(after setting the content type correctly).  This way, you don't have to
mess with the Content-Disposition header (which is the magical part that
doesn't always work perfectly)...  The browser sees the filename in the URL
just as it would if it were a static file on a 'normal' web server.  Works
like a champ, and gets the ASP/PHP/JSP guys green with envy!  Ok, maybe
not... ;)




Jason



reply