How Upload file using Mojolicious?

匆匆过客 提交于 2020-01-01 02:10:32

问题


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.');
}

回答1:


(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.




回答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://stackoverflow.com/a/28605563/4632019




回答3:


You can use Mojolicious::Plugin::RenderFile

Mojolicious::Plugin::RenderFile



来源:https://stackoverflow.com/questions/10152973/how-upload-file-using-mojolicious

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!