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

How to upload a file from POST request. CONTENT_TYPE: application/octet-stream. Perl

我收到 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 并将其上传到服务器?

如果你想post一个文件:

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');

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

$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> };

解决方法在下,那个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;