How Upload file using Mojolicious?
Asked Answered
H

3

15

I have been trying out Mojolicious web framework based on perl. And I have try to develop a full application instead of the Lite. The problem I am facing is that I am trying to upload files to server, but the below code is not working.

Please guide me what is wrong with it. Also, if the file gets uploaded then is it in public folder of the application or some place else.

Thanks in advance.

sub posted {
 my $self = shift;
 my $logger = $self->app->log;

 my $filetype = $self->req->param('filetype');
 my $fileuploaded = $self->req->upload('upload');

 $logger->debug("filetype: $filetype");
 $logger->debug("upload: $fileuploaded");

 return $self->render(message => 'File is not available.')
  unless ($fileuploaded);

 return $self->render(message => 'File is too big.', status => 200)
   if $self->req->is_limit_exceeded;

 # Render template "example/posted.html.ep" with message
 $self->render(message => 'Stuff Uploaded in this website.');
}
Hellfire answered 14/4, 2012 at 10:44 Comment(2)
I do see that in the Mojolicious::Lite documentation the call to $self->req->upload() is wrapped in an if(), which means its return value is being checked before assuming there was an upload. Doing so is the equivalent of checking the return value of open, I suppose.Drucie
Possible duplicate of How to upload multiple files using Mojolicious?Audun
M
10

(First, you need some HTML form with method="post" and enctype="multipart/form-data", and a input type="file" with name="upload". Just to be sure.)

If there were no errors, $fileuploaded would be a Mojo::Upload. Then you could check its size, its headers, you could slurp it or move it, with $fileuploaded->move_to('path/file.ext').

Taken from a strange example.

Mariano answered 14/4, 2012 at 17:9 Comment(3)
Thanks menozero, the HTML part is same as you have mentioned. And I have gone through the link provided, but it is developing based on the Mojolicious::Lite and full application. And in my case $fileuploaded is having empty value, so I am not very sure what is currently going wrong.Hellfire
Works perfectly fine for me. Thanks a lot menozero! This should be accepted as the solution by Kunal Jha. The problem from him is somewhere else...Muncey
Which library should I look at for serving files to be downloaded?Theodor
A
2

To process uploading files you should use $c->req->uploads

post '/' => sub {
   my $c = shift;
   my @files;
   for my $file (@{$c->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);
   }
   $c->render(text => "@files");
} => 'save';

See full code here: https://mcmap.net/q/825764/-how-to-upload-multiple-files-using-mojolicious

Audun answered 15/3, 2019 at 11:34 Comment(0)
F
-2

You can use Mojolicious::Plugin::RenderFile

Mojolicious::Plugin::RenderFile

Ferretti answered 25/4, 2013 at 14:46 Comment(1)
According to the documentation you linked, RenderFile is for streaming downloads, not uploads.Arlenearles

© 2022 - 2024 — McMap. All rights reserved.