Perl 下载后文件为空

Perl 下载后文件为空,perl,download,cgi,Perl,Download,Cgi,我正在尝试从服务器下载csv文件。正在下载文件,但该文件为空。欢迎提出任何建议。文件名为Maintenance_File.csv,位于/home/netcool位置 #!/usr/bin/perl use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); my $files_location; my $ID; my @fileholder; $files_location = "/home/netcool"; #$ID = param

我正在尝试从服务器下载csv文件。正在下载文件,但该文件为空。欢迎提出任何建议。文件名为Maintenance_File.csv,位于/home/netcool位置

#!/usr/bin/perl

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

my $files_location;
my $ID;
my @fileholder;

$files_location = "/home/netcool";

#$ID = param('file');
$ID = "Maintenance_File.csv";
#print "Content-type: text/html\n\n";
#print "ID =$ID";

if ($ID eq '') {
  print "You must specify a file to download.";
} else {
  $fileloc="/home/netcool/" . $ID;
  open(DLFILE, "$fileloc") || Error('open', 'file');
  @fileholder = <DLFILE>;
  close (DLFILE) || Error ('close', 'file');
  #print "Files data = @fileholder";
  print "Content-Type:application/octet-stream;\n";
  print "Content-Disposition:attachment;filename=\"$ID\"\r\n\n";
  print @fileholder
  #open(DLFILE, "< $fileloc") || Error('open', 'file');
  #while(read(DLFILE, $buffer, 100) ) {
  #  print("$buffer");
  #}
  #close (DLFILE) || Error ('close', 'file');

}
#/usr/bin/perl
使用CGI“:标准”;
使用CGI::Carp qw(fatalsToBrowser);
我的$U位置;
我的$ID;
我的@fileholder;
$files_location=“/home/netcool”;
#$ID=param('file');
$ID=“Maintenance\u File.csv”;
#打印“内容类型:text/html\n\n”;
#打印“ID=$ID”;
如果($ID eq“”){
打印“您必须指定要下载的文件。”;
}否则{
$fileloc=“/home/netcool/”$ID;
打开(DLFILE,“$fileloc”)| |错误('open','file');
@文件持有人=;
关闭(DLFILE)| |错误('close','file');
#打印“文件数据=@fileholder”;
打印“内容类型:应用程序/八位字节流;\n”;
打印“内容处置:附件;文件名=\”$ID\“\r\n\n”;
打印@fileholder
#打开(DLFILE,“<$fileloc”)| |错误('open','file');
#while(读取(DLFILE,$buffer,100)){
#打印(“$buffer”);
#}
#关闭(DLFILE)| |错误('close','file');
}

您的代码工作正常。我不知道为什么它在你的环境中不起作用,但它在我的环境中起到了预期的作用。也许你可以分享更多关于你运行它的环境的信息

  • 您使用的是什么操作系统
  • 您使用的是什么web服务器
  • 是否有任何内容写入web服务器错误日志
您的代码使用了许多过时的构造。重写为更现代的Perl,如下所示:

#!/usr/bin/perl

use strict;
use warnings;

use CGI 'header';
use CGI::Carp qw(fatalsToBrowser);

my $files_location = "/home/netcool";

my $filename = 'Maintenance_File.csv';

if (!$filename) {
  die "You must specify a file to download";
  exit;
}

print header(
  -type => 'application/octet-stream',
  -content_disposition => "attachment;filename=$filename",
);

my $fileloc = "$files_location/$filename";
open my $fh, '<', $fileloc or Error('open', 'file', $!);
print while <$fh>;
close $fh or Error ('close', 'file' );

sub Error {
  die "@_";
}
#/usr/bin/perl
严格使用;
使用警告;
使用CGI“标题”;
使用CGI::Carp qw(fatalsToBrowser);
我的$files\u location=“/home/netcool”;
我的$filename='Maintenance_File.csv';
如果(!$filename){
die“您必须指定要下载的文件”;
出口
}
打印页眉(
-类型=>'应用程序/八位字节流',
-content\u disposition=>“附件;文件名=$filename”,
);
my$fileloc=“$files\u location/$filename”;

打开我的$fh,'当我在新安装的Ubuntu上设置它并做了一些微小的调整(因为我没有名为
/home/netcool/Maintenance\u file.csv的文件),它完全按照预期工作。因此,问题在于你没有向我们展示的东西。你在哪个系统上运行这个?是否有任何内容写入web服务器错误日志?我的意思是,我可以建议对代码进行各种改进(为什么不使用
use strict
use warnings
?为什么加载CGI.pm然后不使用它?为什么要在2019年编写CGI程序?),这种方法有效。感谢@Dave Cross的评论。代码正在运行,请接受以下更改。打印标题(-type=>'text/csv',-content\u disposition=>“attachment;filename=$filename”,);现在,假设我在网页上给出了两个文本框,并根据输入,需要形成文件名,然后进行下载。下面的代码不适用于我的$files\u location=“/home/netcool”;my$first=$cgi->param('first_name');my$last=$cgi->param('last_name');打印“内容类型:text/html\n\n”;print@SaurabhJain:如果有人回答了你的问题,那么你应该接受他们的答案。如果你有后续问题,请在新帖子中提问。