#!/usr/bin/perl -w
use strict;
use warnings;
=NOTES
This code shows how to stream a file to a remote WWW user agent
in the form of a file download. Tommy Butler - 2/7/03
This program is free software, you may redistribute and/or modify it under
the terms of the GNU GPL. <URL: http://www.gnu.org/licenses/gpl.txt>
=cut
# use the CGI library header routine
use CGI qw/ header /;
# type in the name of the file you want to send out...
my($file) = './downloads/presentation.pdf';
# you _must_ call binmode on STDOUT for binary file streams
binmode(STDOUT);
# send appropriate HTTP headers
print header (
'-status' => '200 OK',
'-type' => 'application/octet-stream',
'-attachment' => ( split(/\\|\/|\:/,$file) )[-1],
'-Content_length' => -s $file,
);
# stream file to remote user agent
print &load_file($file);
# subroutine that loads a file into memory and returns it's contents
sub load_file {
my($f) = shift(@_);
local(*FH,$/);
open(FH, $f) or die(qq[Failed while trying to open "$f" $!]);
$f = <FH>;
close(FH);
return($f);
}