尝试使用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
结果: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
,但是当检查文件夹Ojit_code时,它包含两个上载的两个文件。问题是我如何将两个文件都保存到脚本中,而不是最后一个?
最佳答案
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对象的技术来保存给定键的备用值。
关于perl - 使用Plack处理多个文件上传,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18166156/