如何使用perl在浏览器上显示二进制数据?

时间:2013-10-03 11:14:59

标签: perl apache2 cgi large-files

我正在尝试将文件的内容(原始内容)发送到浏览器。 但是文件是作为附件提供的,即使标题中没有附件也是如此。 任何人都可以建议我如何使用perl在浏览器上显示原始数据?

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use File::Copy qw( copy );
use File::Spec::Functions qw( catfile );
use POSIX qw(strftime);
use Time::Local;
use HTTP::Status qw(:constants :is status_message);
use Digest::MD5 qw(md5 md5_hex md5_base64);
use File::Basename;
use URI;



my $extfile = '/home/suresh/clientrequest.txt';
open(FH, ">>$extfile") or die "Cannot open file";
my $query = CGI->new;
my $stcode = status_message(206);
my $uri =$ENV{'REQUEST_URI'};
my $rdate =strftime("%a, %d %b %Y %H:%M:%S %Z", localtime());
print FH "Client request: ", $ENV{'REQUEST_URI'}, "\n";
my $dir  = '/srv/samba/Assets';
#my $dir  = '/home/suresh/Assets';
my $nffFile = fileparse ("$uri", qr/\.[^.]*/);
my $fullFname = $nffFile . ".nff";
my $path = catfile($dir, $fullFname);
my $filesize = -s $path;
print FH "Size of the file: ", $filesize, "\n";

#Search requested asset files
opendir(DIR, $dir);
my @files = readdir(DIR);
if (grep($_=~/$fullFname/,@files)){
print FH "Found the requested NFF file: ", $fullFname, "\n"; 
open my $fh, '<:raw', $path;
print "$ENV{SERVER_PROTOCOL} 206 $stcode";
print $query->header(
    -'Accept-Range'=>'bytes',
    -'Date'=> $rdate,
    -'Content-Range'=>'0-188/$filesize',
    -'Content-Length'=>$filesize,
    -'Content-Type'=>'application/octet-stream',
    -'Connection'=>'Keep-Alive',
    -'Media-Type'=>'application/octet-stream',
    );
binmode STDOUT, ':raw';
    copy $fh => \*STDOUT;
    close $fh
        or die "Cannot close '$path': $!";

}else {
    print $query->header('text/plain', '404 File not Found!');
    print FH "Requested NFF file: ", $fullFname, " not found!!\n\n";
    }
closedir(DIR);

1 个答案:

答案 0 :(得分:0)

已解决..这是一个简单的更改,我需要将内容类型更改为text / html以存档。