0

我无法在 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

文件.txt

我好吗,你好吗?

4

3 回答 3

3

在第 4 行你有:

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

双重\n表示标题结束,内容将随之而来。摆脱它,然后再试一次?

于 2013-10-17T10:59:19.250 回答
0

尝试评论

    print "Content-type:text/html\n\n";
于 2013-10-17T11:13:18.950 回答
0

您必须将下面的行从顶部移动到 if 循环内部:

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.";  
}
于 2016-10-23T09:23:39.123 回答