-
-
Save bimmerlabs/85bc705995df98c4b2ee103eacb7bed6 to your computer and use it in GitHub Desktop.
The current example of http://mojolicious.org/perldoc/Mojolicious/Guides/Tutorial#File-uploads doesn't work as expected - if you submit the form with no file selected, it doesn't redirect back to the form. It's also not clear what happens to the file after it's been submitted (I added some clues as to where to look)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
# Upload form in DATA section | |
get '/' => 'form'; | |
# Multipart upload handler | |
post '/upload' => sub { | |
my $c = shift; | |
# Check file size | |
return $c->render(text => 'File is too big.', status => 200) | |
if $c->req->is_limit_exceeded; | |
my $upload = $c->param('upload'); | |
# "my $upload = $c->param('upload')" returns true even if there is no file submitted | |
# so the redirect_to('form') never happens. | |
# use "my $size = $upload->size" instead of "my $upload = $c->param('upload')" | |
# Process uploaded file with Mojo::Upload | |
return $c->redirect_to('form') unless my $size = $upload->size; | |
my $name = $upload->filename; | |
my $file = $upload->asset; | |
# Move asset data into a file with Mojo::Asset::File | |
$file = $file->move_to("/home/sri/$name"); | |
$c->render(text => "Thanks for uploading $size byte file $name."); | |
}; | |
app->start; | |
__DATA__ | |
@@ form.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title>Upload</title></head> | |
<body> | |
%= form_for upload => (enctype => 'multipart/form-data') => begin | |
%= file_field 'upload' | |
%= submit_button 'Upload' | |
% end | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment