使用 Plack 处理多个文件上传 [英] Handling multiple file uploads with Plack
问题描述
尝试使用 Plack 处理多个文件上传.
Trying handling multiple file uploads with Plack.
我的表格:
<form id="file_upload" action="savefile" method="POST" enctype="multipart/form-data">
<input type="file" name="file[]" multiple>
<button>upload</button>
</form>
选择了两个文件,名为:x1
和 x2
.Data::Dumper
的结果:
Selected two files, called: x1
and x2
. The Data::Dumper
result of the:
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/
时,它包含两个文件 已上传.
So, it contains only the second file x2
, but when checked the folder /var/folders/7l/nhyscwy14bjb_sxr_t2gynpm0000gn/T/
it contains both files uploaded.
问题是我如何才能将两个文件添加到脚本中,而不是最后一个?
The question is how i can get both files to the script, not ony the last one?
推荐答案
for my $upload ($req->upload('file[]')) {
$upload->filename;
}
您也可以调用 @uploads = $req->uploads->get_all('file[]')
来获取多个值.
You can also call @uploads = $req->uploads->get_all('file[]')
to get multiple values.
参见 perldoc Plack::Request
(和 Hash::MultiValue
) 了解更多详情.
See perldoc Plack::Request
(and Hash::MultiValue
) for more details.
您在 Data::Dumper 中看不到它们的原因是 Hash::MultiValue 使用一种称为内向外对象的技术,为给定的键保存替代值.
The reason you don't see them in Data::Dumper is that Hash::MultiValue uses a technique called inside-out object, to save alternate values for a given key.
这篇关于使用 Plack 处理多个文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!