2013-10-17 193 views
0

我無法在cgi perl下載該文件。相反,我得到的內容打印在網頁上。這是我試過的。在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的

IH如何爲U IAM HRE

回答

3

第4行中你有:

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

這雙\n信號頭結束,內容將隨之而來。擺脫這一點,然後再試一次?

+0

不過我得到的same.No變化 – Nagaraju

+0

設我更具體。請在腳本中註釋掉或完全刪除第4行。仍然是相同的結果? –

+0

我得到內部服務器錯誤 – Nagaraju

0

嘗試評論

print "Content-type:text/html\n\n"; 
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."; 
}