Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do i force the user to download my content?

( #159163=categorized question: print w/ replies, xml ) Need Help??
Contributed by BUU on Apr 15, 2002 at 13:15 UTC
Q&A  > CGI programming


Description:

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

Answer: How do i force the user to download my content?
contributed by merlyn

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.

Answer: How do i force the user to download my content?
contributed by Joost

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.

Answer: How do i force the user to download my content?
contributed by BUU

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.

Answer: How do i force the user to download my content?
contributed by simon.proctor

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.

Answer: How do i force the user to download my content?
contributed by chicks

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
Answer: How do i force the user to download my content?
contributed by BUU

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.

Answer: How do i force the user to download my content?
contributed by little

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. :-)

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2014-11-23 19:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (134 votes), past polls