[thelist] Re: Browser detection and save file as

Kevin Martin evolt at brasscannon.net
Wed Sep 19 08:09:52 CDT 2001


Thanks to Greg's pointer on how Cold Fusion does it, I was able to make
a perl CGI to do the same thing.  It's basically a shotgun marriage of
my very first CGI (provided here by a kind soul) with a perl push-a-file 
routine that is normally used to send CA certificates. :-)

Got a tip from Microsoft on how to fix the content-disposition header,
to override IE's insistence on saving the file as 'download.cgi' -- 
it now appears to work in both IE 5 and Netscape.

It needs some bulletproofing, of course, but here's what it looks like:

8<------ clip here
#!/usr/bin/perl
## Syntax: http://mysite/cgi-bin/download.cgi?username=filename
@params=split(/=/,$ENV{'QUERY_STRING'});
my $user_dir = "/home/www/$params[0]";
my $user_file = $params[1];
my $send_file = "$user_dir/$user_file";

##  Uncomment to debug in browser:
#print "Content-type: text/html\r\n\r\n";
#print "<html><body>";
#print "P0: ", $params[0], " P1: ", $params[1], "\r\n";
#print "SF: ", $send_file, "\r\n";
#print "</body></html>";
##

my $data = "";
open(AFILE, "<$send_file");
while(<AFILE>) { $data .= $_; }
close(AFILE);
print "Content-type: application/x-unknown\r\n";
print "Content-disposition: inline; filename= ", $user_file, "\r\n";
print "Content-length: ", length($data), "\r\n\r\n$data";

1;

# end of download.cgi
8<------ clip here

-- 
Kevin "digger" Martin <evolt at brasscannon.net>





More information about the thelist mailing list