Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Handling multiple file upload with Mojo

by trippledubs (Deacon)
on Nov 05, 2014 at 14:46 UTC ( #1106210=perlquestion: print w/replies, xml ) Need Help??

trippledubs has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Is there a way to handle multiple file uploads on the server side? i want the user to be able to select an unlimited amount of files and upload them all at one time. I realize that all browsers in use may not support the multiple tag.

#!/usr/bin/env perl use strict; use warnings; use Mojolicious::Lite; get '/upload' => sub { my $c = shift; $c->render('simpleUploadForm'); }; app->start; __DATA__ @@ simpleUploadForm.html.ep <!DOCTYPE html> <html> <form action='/handleUpload' autocomplete="off"> Select Files: <input type="file" name="img" multiple> <input type="submit"> </html>

Replies are listed 'Best First'.
Re: Handling multiple file upload with Mojo
by blindluke (Hermit) on Nov 05, 2014 at 15:32 UTC

    Take a look at the documentation for upload and every_upload methods. Your code would look similar to this:

    post '/' => sub { my $c = shift; my @files; for my $file ($c->req->upload('files')) { push @files, $file->filename; } $c->render(text => "@files"); } => 'save';

    Look here for an answer to a similar question.

    - Luke

      That is great, thank you.
Re: Handling multiple file upload with Mojo
by trippledubs (Deacon) on Nov 06, 2014 at 02:26 UTC
    Just to make this code complete with working example. Did not know this, but Mojolicious has no dependencies, and also CGI is out of Perl core.
    #!/usr/bin/env perl use strict; use warnings; use Mojolicious::Lite; my $uploadFileDirectory = 'UPLOADS'; mkdir $uploadFileDirectory if ( !-d $uploadFileDirectory ); $ENV{"MOJO_MAX_MESSAGE_SIZE"} = 25 * 2**20; #25 MB get '/upload' => sub { my $c = shift; $c->render('simpleUploadForm'); }; post '/handleUpload' => sub { my $c = shift; my @fileNames; my $files = $c->req->every_upload('files'); for my $file ( @{$files} ) { my $fileName = $file->filename =~ s/[^\w\d\.]+/_/gr; $file->move_to("$uploadFileDirectory/$fileName"); $c->app->log->debug("$fileName uploaded\n"); push @fileNames, $fileName; } $c->render( 'successUpload', files => \@fileNames ); }; app->start; __DATA__ @@ simpleUploadForm.html.ep <!DOCTYPE html> <html> <form action='/handleUpload' method='post' enctype='multipart/form- +data'> Select Files: <input type='file' name='files' multiple=1><br/> <input type="submit" > </html> @@ successUpload.html.ep <!DOCTYPE html> <html> <img src='success.jpg' /> <% for my $j (@{$files}) { %> <p> <%= $j %> </p> <% } %> </html> @@ exception.development.html.ep <!DOCTYPE html> <html> Fail </html>
    Update: Must use method "every_upload" introduced in Mojolicious 5.48 or you will only get one file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1106210]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-11-28 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?