如何使用Mojolicious上传文件? [英] How Upload file using Mojolicious?

查看:228
本文介绍了如何使用Mojolicious上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试用基于Perl的Mojolicious web框架。我已经尝试开发一个完整的应用程序,而不是Lite。我正面临的问题是,我试图上传文件到服务器,但下面的代码不工作。



请指导我什么是错的。此外,如果文件被上传,那么它是在应用程序的公共文件夹或其他地方。

预先感谢。

  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);
$ b $ return $ self> render(message =>'File is not available。')
除非($ fileuploaded);

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

#使用消息
$ self->渲染模板example / posted.html.ep渲染(消息=>'在本网站上载的东西'。


解决方案

使用 method =post enctype =multipart / form-data以及 input type =file with name =upload



如果没有错误, $ fileuploaded 将会是 Mojo :: Upload 。然后你可以用 $ fileuploaded-> move_to('path / file.ext')来检查它的大小,标题, p>

奇怪的例子


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

解决方案

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

这篇关于如何使用Mojolicious上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆