Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple files using Mojolicious?

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>
like image 213
waghso Avatar asked Feb 19 '15 09:02

waghso


2 Answers

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>
like image 162
waghso Avatar answered Sep 22 '22 01:09

waghso


I think there is a small bug in the answer above - the variable ProcessingFolder is obsolete ...

#!perl -w
# install Mojolicious by : sudo -s 'curl -L cpanmin.us | perl - Mojolicious'
# run by : perl mojo-file-upload.pl daemon -m production -l http://*:8083
# point your browser @: http://127.0.0.1/upload-files
#
use Mojolicious::Lite;
use Mojo::Upload;
use v5.14;

get '/' => 'upload-files-page';
get '/upload-files' => 'upload-files-page';
post '/upload-files' => 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("/tmp/".$name);
   }
   $self->render(text => "@files");
} => 'save';

app->start;

__DATA__


@@ upload-files-page.html.ep
<!DOCTYPE html>
<html>
   <body>
   <form action="/upload-files" 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>
like image 40
Yordan Georgiev Avatar answered Sep 23 '22 01:09

Yordan Georgiev