【问题标题】:Handling multiple file uploads with Plack使用 Plack 处理多个文件上传
【发布时间】:2013-08-10 20:40:57
【问题描述】:

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

我的表格:

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

选择了两个文件,名为:x1x2Data::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/ 时,它包含两个文件已上传。

问题是我如何才能将两个文件添加到脚本中,而不是最后一个?

【问题讨论】:

    标签: perl plack


    【解决方案1】:
    for my $upload ($req->upload('file[]')) {
      $upload->filename;
    }
    

    您也可以调用@uploads = $req-&gt;uploads-&gt;get_all('file[]')获取多个值。

    请参阅perldoc Plack::Request(和Hash::MultiValue)了解更多详情。

    您在 Data::Dumper 中看不到它们的原因是 Hash::MultiValue 使用一种称为 inside-out object 的技术来保存给定键的备用值。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 2021-09-25
      • 2011-09-03
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多