http://qs321.pair.com?node_id=159163

BUU has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

What can i do to force the browser to open up a "Save As" Dialog to save what my script prints out?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do i force the user to download my content?
by merlyn (Sage) on Apr 15, 2002 at 14:29 UTC
    There's no standard for that. "FORCE" doesn't work on the internet. I could have my browser to automatically display everything that is application/octet-stream, and in fact, Mac IE seems to display JPEGs as images even when they are tagged as application/octet-stream.

    So, the proper answer is user education ("Use your browser to save this link") and not gimmicky tricks to try to defeat the user.

Re: How do i force the user to download my content?
by Joost (Canon) on Apr 22, 2002 at 12:43 UTC
    As per RFC 2616 (HTTP/1.1), you might try setting the following headers:

    Content-Disposition: attachment; filename="fname.ext" Content-Type: application/octet-stream

    Note:
    In practice the content-disposition only specifies a preferred filename for when you choose "Save as" from your browser-menu.

    You also need to set content-type to application/octet-stream if you want the browser to "force" a "Save file as..." popup instead of it trying to display the data first.

    Joost.

Re: How do i force the user to download my content?
by BUU (Prior) on Apr 15, 2002 at 13:16 UTC
    You simply return a "Content-type: application/octet-stream\n\n" as opposed to the normal "Content-type:text/html\n\n", and this opens up a "Save As" Dialog box, and saves stdout to their computer.
Re: How do i force the user to download my content?
by simon.proctor (Vicar) on Apr 15, 2002 at 13:42 UTC
    As a supplement to the above answer, you may also want to look at the content-disposition header. Some browsers do not use the filename that you wish and so this gives you the opportunity (browser willing) to remedy that.
Re: How do i force the user to download my content?
by chicks (Scribe) on Apr 21, 2002 at 15:39 UTC
    It can also be helpful to put the preferred file name on the end of the original CGI query string so that it will be used for the "save as" file name, like:
      http://www/somecgi?greatstuff.pdf
Re: How do i force the user to download my content?
by BUU (Prior) on Apr 16, 2002 at 13:47 UTC
    Perhaps 'force' is a bad word in this context. Your not trying to 'defeat your users' by giving them a 'save as' dialog box, your merely trying to make it easier for them to conduct their business, whatever that happens to be, by not forcing them to trying to figure out the pecularities of that browsers 'save target as' function.
Re: How do i force the user to download my content?
by little (Curate) on Jul 15, 2002 at 13:05 UTC
    A browser requests a document, and in return receives the answer from the server. The Browser remits a mime-type-list of which types he is able to view or in any other way to process. If a server would send a browser a mime-type declaration for a document to which the browser has no application assigned to handle it, (yes this is badly faking and not conformant to any standard as it only works in breaking the standard) then the browser would offer the download to the user. eg. you send something like "Content-Type: application/cherry-pie" no browser would know what to do with a file of such format, hence offering to save it. But DON'T do that if you respect your visitors :-) They would probably dislike the need of renaming files and fear that you'd just 'forced' them into downloading malicious code. :-)
Re: How do i force the user to download my content?
by DSPerlCoder (Novice) on Jul 14, 2002 at 21:01 UTC
    An easy way to have the Download dialog box open up is to set a meta tag for refresh and set the url to the file.

    Example: <meta http-equiv=refresh content=0;url=someFile.ext>

    Originally posted as a Categorized Answer.