在cgi perl脚本中下载文件

时间:2013-10-17 10:50:31

标签: perl cgi download

我无法在cgi perl中下载该文件。相反,我得到了网页上打印的内容。这就是我尝试过的。

代码:

use CGI qw /:standard /;
use CGI;

print "Content-type:text/html\n\n";
my $files_location;
my $ID;
my @fileholder;

$directorypath = "/var/www/cgi-bin/";        
$files_location = $directorypath;
$ID = 'file.txt';
#$ID = param('ID');  

if ($ID eq '') {  
print "You must specify a file to download.";  
} else {  

open(DLFILE, "<$files_location/$ID") || Error('open', 'file');  
@fileholder = <DLFILE>;  
close (DLFILE) || Error ('close', 'file');  

#these are the html codes that forces the browser to open for download  
print "Content-Type:application/x-download\n";  
print "Content-Disposition:attachment;filename=$ID\n\n";  
print @fileholder;  
}

我收到了这个:

Content-Type:application/x-download Content-Disposition:attachment;filename=file.txt ih how are u iam hre

file.txt的

我怎么样?

3 个答案:

答案 0 :(得分:3)

第4行你有:

print "Content-type:text/html\n\n";

\n信号标题结尾,内容将跟随。摆脱它,然后再试一次?

答案 1 :(得分:0)

尝试评论

    print "Content-type:text/html\n\n";

答案 2 :(得分:0)

如果循环:

,您必须将以下行从上到下移动
print "Content-type:text/html\n\n";

if ($ID eq '') {  
print "Content-type:text/html\n\n";
print "You must specify a file to download.";  
}