in reply to File Download from CGI Script
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;
|
|---|