Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

How do i force the user to download my content?

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


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

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.


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:

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [marioroy]: Corion, start and start_child in MCE::Hobo::Manager return a MCE::Hobo object, whereas P::FM returns the PID. I can have it return the PID though. I tried Hobo::Manager with several P::FM modules, just changed P::FM to MCE::Hobo::Manager and it works.
    [marioroy]: I also have a Hobo driver for Forklift allowing folks to use in multiple classes, no conflicts with one another. That's not possible for P::FM.
    [Discipulus]: congrats marioroy!
    [marioroy]: CORE::wait works well eventhough multiple instances or classes using Hobo::Manager.
    [Corion]: marioroy: I'm not sure what the normal use for the PID is in P:FM, but I guess that most programs just ignore or log it
    [Corion]: Oh, yes, programs could call wait $pid, but if your $pid is an object, then you could add a ->wait method to it and wait $pid would call that automatically "thanks" to indirect object notation
    [marioroy]: Just documentation edits is all that remains. Hobo::Simple provides foreach and forseq with identifier capability -- all transparently supporting array, hash, file handle, and seq 1 .. N.
    [marioroy]: Corion Regarding PID, that's great. So will leave it so compatible with MCE::Hobo. e.g. ->create returns a Hobo object. Folks can get ->pid from it. So, that's not a problem.
    [choroba]: ad readdir: 5.12 needed

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2017-05-26 08:44 GMT
    Find Nodes?
      Voting Booth?