使用Plack处理多个文件上传

8

尝试使用Plack处理多个文件上传。

我的表单:

<form id="file_upload" action="savefile" method="POST" enctype="multipart/form-data">
<input type="file" name="file[]" multiple>
<button>upload</button>
</form>

选择了两个文件,分别命名为:x1x2。使用Data::Dumper对其进行了结果输出:

my $u = $req->uploads;

$VAR1 = bless( {
    'file[]' => bless( {
         'headers' => bless( {
              'content-disposition' => 'form-data; name="file[]"; filename="x2"',
              'content-type' => 'application/octet-stream',
              '::std_case' => {
                   'content-disposition' => 'Content-Disposition'
              }
         }, 'HTTP::Headers' ),
         'filename' => 'x2',
         'tempname' => '/var/folders/7l/nhyscwy14bjb_sxr_t2gynpm0000gn/T/7vt04wIrne',
         'size' => 146
    }, 'Plack::Request::Upload' )
}, 'Hash::MultiValue' );

所以,它只包含第二个文件x2,但是当检查文件夹/var/folders/7l/nhyscwy14bjb_sxr_t2gynpm0000gn/T/时,它包含上传的两个文件
问题是如何将两个文件都传递给脚本,而不仅仅是最后一个文件?
1个回答

14
for my $upload ($req->upload('file[]')) {
  $upload->filename;
}

你还可以调用@uploads = $req->uploads->get_all('file[]')来获取多个值。

有关更多详细信息,请参见perldoc Plack::Request (以及Hash::MultiValue)。

之所以在Data::Dumper中看不到它们,是因为Hash::MultiValue使用了一种称为inside-out object的技术,用于保存给定键的备用值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接