【问题标题】:Downloading file in cgi perl script在 cgi perl 脚本中下载文件
【发布时间】:2016-10-23 17:23:39
【问题描述】:

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

你好吗

【问题讨论】:

    标签: perl cgi download


    【解决方案1】:

    在第 4 行你有:

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

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

    【讨论】:

    • 我还是一样。没有变化
    • 让我说得更具体一些。请注释掉或完全删除脚本中的第 4 行。结果还是一样?
    • 好的,当我将该下载代码复制到名为 download.pl 的脚本中并将以下行添加到我的原始脚本打印“

      下载文件

      \n";
    【解决方案2】:

    尝试评论

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

    【讨论】:

      【解决方案3】:

      您必须将下面的行从顶部移动到 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.";  
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-28
        • 2015-05-27
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        • 2012-03-10
        相关资源
        最近更新 更多