如何从POST请求上传文件。 CONTENT_TYPE:application / octet-stream。 Perl的

时间:2017-09-15 07:14:17

标签: perl cgi postdata

我收到了

的POST请求
CONTENT_TYPE: application/octet-stream

我得到了这样的所有数据

my $cgiQuery = CGI->new() or die();
my $cgiData = $cgiQuery->Vars;

my $getQuery = CGI->new($ENV{QUERY_STRING});
my $getData = $getQuery->Vars;

foreach my $getKey(keys %{$getData})
{
    $cgiData->{$getKey} = $getData->{$getKey};
}

my $data = $cgiQuery->param('POSTDATA');

但是当我尝试打印时

print Dumper($data) ."\n";
print Dumper($cgiData) ."\n";

我明白了:

$VAR1 = 'type=catalog&mode=file&filename=offers0_14.xml';

$VAR1 = {
      'POSTDATA' => 'type=catalog&mode=file&filename=offers0_14.xml',
      'session' => 'aa4979ad18f64e4d959dd16444cee5fd'
    };

如何获取文件filename = offers0_14.xml并将其上传到服务器?

2 个答案:

答案 0 :(得分:2)

如果您想发布文件:

open(my $fh, '<:raw', $qfn)
   or die $!;

my $file = do { local $/; <$fh> };

$ua->post($uri,
   Content_Type => 'application/octet-stream',
   Content => $file,
);

收到它:

my $file = $cgi->param('POSTDATA');

如果您要发布包含文件上传字段的表单:

$ua->post($uri,
   Content_Type => 'form-data',
   Content => [
      type  => 'catalog',
      mode  => 'file',
      file  => [ $qfn ],
   ],
);

收到它:

my $fn = $cgi->param('file');  # Not safe to use!!!
my $fh = $cgi->upload('file');
my $file = do { local $/; <$fh> };

答案 1 :(得分:0)

接下来的解决方案是,POSTDATA是二进制文件。所以,我通过这段代码得到了那个文件

my $file_cgi = CGI->new($ENV{POSTDATA});
my $file = $file_cgi->Vars;

my $filename = path_to_the_file_on_the_server;    
open (OUTFILE,"> $filename");
print OUTFILE $file->{POSTDATA};
close OUTFILE;