How to upload multiple files using Mojolicious?

大城市里の小女人 提交于 2019-12-07 07:49:42

问题


I'm new to Mojolicious, trying to learn it. I'm trying to upload multiple files using form but only one file is uploaded at a time. Any suggestion?

#!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>

回答1:


Found Answer. Used uploads() instead of upload() which returns Array reference.

#!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>


来源:https://stackoverflow.com/questions/28602910/how-to-upload-multiple-files-using-mojolicious

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!