Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Handling multiple file upload with Mojo by trippledubs
in thread Handling multiple file upload with Mojo by trippledubs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-12-03 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (29 votes). Check out past polls.

    Notices?