The headers look ok. I tested it successfully with IE6 and Firefox1.5 on a WinXP machine with slight modifications and a sample .tgz file:
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'pm.tgz';
my $filesize = -s $filename;
# $filepath is the directory
# $filename is the name of the file
# print full header
print "Content-disposition: inline; filename=$filename\n";
print "Content-Length: $filesize\n";
print "Content-Type: application/octet-stream\n\n";
# open in binmode
open(READ,,'<',$filename) || die;
binmode READ;
# stream it out binmode
binmode STDOUT;
while (<READ>) {
print;
}
close(READ);
# should always return true
return(1);
Is it possible that you printed a header before this function was called?
I would recommend the following minor changes :
- Use scalars for filehandles instead of barewords. Stricter scope, and auto-closes the file when the scalar is destroyed
- Use the 3 argument form of open
- Use 'or' instead of '||' for testing failure as it has lower precedence
Updated code:
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'pm.tgz';
my $filesize = -s $filename;
# $filepath is the directory
# $filename is the name of the file
# print full header
print "Content-disposition: inline; filename=$filename\n";
print "Content-Length: $filesize\n";
print "Content-Type: application/octet-stream\n\n";
# open in binmode
open my $read,'<',$filename or die "Failed to read $filename - $!";
+
binmode $read;
# stream it out binmode
binmode STDOUT;
while (<$read>) {
print;
}
# should always return true
return(1);
|