Greetings Anonymous,

It's fairly impossible to make sure that every browser "downloads" a file off the Web rather than rendering it since by design the browser is expected to be smart about such things. (Of course, in reality, everything is downloaded. So we're talking about displaying the file in the browser or presenting the user with the option to save the file.) What we can do is set it up for the most likely successful situation and hope the users have smart browsers.

First off, you're dealing with CGI via Perl, so therefore you should always use the CGI module. At this point, it's just a matter of picking desired behaviors. I've noticed that I tend to get better results if I've been specific about the type of data I want to transmit. For example, here's how I push out Excel documents.

use CGI; my $query = new CGI; print $query->header( -type => 'application/vnd.ms-excel', -Content_Disposition => 'attachment' );

This seems to work in both Navigator, IE, and Mozilla. In IE, the browser tries to be super smart and render the content inside the browser. Netscape and Mozilla simply ask the user what /s{0,1}he/ wants to do (save or export to other application).

For your specific situation, I'd use "octet-stream" without any mention of the filename. Perhaps not even have the content disposition included.

print $query->header( -type => 'application/octet-stream', -Content_Disposition => "attachment" );

However, keep in mind that a text file is about as basic to the Web as an HTML file. So there will be browsers out there that do completely unexpected things based on your code. Be sure to always test in all browsers you want to support.

-gryphon
code('Perl') || die;


In reply to Re: File Download from CGI Script by gryphon
in thread File Download from CGI Script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.