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
Titus Brown
2003-12-13
-> I would like to create a form where the user enters a from date, a to date.
-> When the user submits this form, I would like Quixote to generate a vCalendar
-> file and present the user with a "file save as" dialog, defaulting the file
-> name to calendar.ics.
->
-> I tried setting the response content type to text/plain in the form's action
-> method, but this didn't do (even a piece) of what I want.  Internet explorer
-> displays this as plain text in the browser, and when I do a file save as, the
-> text file has html tags in it, which is no good.
->
-> I'm using the old form framework.  Any pointers on how I can get the behavior
-> I want?

Here's code that I use; I haven't done any systematic tests, but it works
on Netscape/Mozilla browsers and Safari too.

'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


reply