gpt4 book ai didi

perl - 如何使用 Mojolicious 上传多个文件?

转载 作者:行者123 更新时间:2023-12-03 14:29:47 24 4
gpt4 key购买 nike

我是 Mojolicious 的新手,正在尝试学习它。我正在尝试使用表单上传多个文件,但一次只上传一个文件。有什么建议吗?

#!perl -w

use Mojolicious::Lite;
use Mojo::Upload;
use v5.14;

get '/' => 'page';
post '/' => sub {
my $self = shift;
my @files;
for my $file ($self->req->upload('files')) {
my $size = $file->size;
my $name = $file->filename;

push @files, "$name ($size)";
$file->move_to("C:\\Program Files\\Apache Software Foundation\\Apache24\\htdocs\\ProcessingFolder\\".$name);
}
$self->render(text => "@files");
} => 'save';

app->start;

__DATA__

@@ page.html.ep
<!DOCTYPE html>
<html>
<body>
<form action="<%=/ProcessingFolder/%>" method="POST"
enctype="multipart/form-data">
<input name="files" type="file" enctype="multipart/form-data" multiple="multiple">
<button type="submit">Upload</button>
</form>
</body>
</html>

最佳答案

找到答案。二手 uploads()而不是 upload()它返回数组引用。

#!perl -w

use Mojolicious::Lite;
use Mojo::Upload;
use v5.14;

get '/' => 'page';
post '/' => sub {
my $self = shift;
my @files;
for my $file (@{$self->req->uploads('files')}) {
my $size = $file->size;
my $name = $file->filename;

push @files, "$name ($size)";
$file->move_to("C:\\Program Files\\Apache Software Foundation\\Apache24\\htdocs\\ProcessingFolder\\".$name);
}
$self->render(text => "@files");
} => 'save';

app->start;

__DATA__

@@ page.html.ep
<!DOCTYPE html>
<html>
<body>
<form action="<%=/ProcessingFolder/%>" method="POST"
enctype="multipart/form-data">
<input name="files" type="file" enctype="multipart/form-data" multiple="multiple">
<button type="submit">Upload</button>
</form>
</body>
</html>

关于perl - 如何使用 Mojolicious 上传多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com