如何使用Mason来实现文件上传

理解HTML文件上传与处理
本文详细介绍了HTML上传表单的基本结构与提交处理方法,包括使用CGI与mod_perl两种方式来处理文件上传,强调了mod_perl的优越性,并提供了一个实际应用示例。

The basic HTML for an upload form looks like:

<form action="..." method="post" enctype="multipart/form-data"> Upload new file: <input name="userfile" type="file" class="button"> <input type="submit" value="Upload">

The way you handle the submission depends on which args method you chose for the ApacheHandler class.
Under the 'CGI' method (default for 1.0x), you can use the $m->cgi_object method to retrieve a CGI.pm object which can be used to retrieve the uploaded file. Here is an example using the 'CGI' method:

<%init> my $query = $m->cgi_object; # get a filehandle for the uploaded file my $fh = $query->upload('userfile'); # print out the contents of the uploaded file while (<$fh>) { print; } close($fh); </%init>

Please see the CGI.pm documentation for more details.
Under the 'mod_perl' method (default for 1.1x), the request object available as $r in your components will be an object in the Apache::Request class (as opposed to the Apache class). This object is capable of returning Apache::Upload objects for parameters which were file uploads. Please see the Apache::Request documentation for more details. Here is an example using the 'mod_perl' method:

<%init> # NOTE: If you are using libapreq2 + mod_perl2 + Apache 2, # you will need to uncomment the following line: # use Apache::Upload; # you can store the file's contents in a scalar my $file_contents; # create an Apache::Upload object my $upload = $r->upload; # get a filehandle for the uploaded file my $upload_fh = $upload->fh; while(<$upload_fh>) { # loop through the file and copy each line to $file_contents $file_contents .= $_; } close($upload_fh); </%init>

For more information on how to manually set the args method, see the ApacheHandler documentation.
If you are using CGI.pm, there are some configuration issues to be aware of. CGI.pm needs a tmp directory, and you probably want to be able to specify what that directory is.
Try doing this in your httpd.conf or handler.pl:

<Perl> use CGI qw(-private_tempfiles); </Perl>

You must do this before you load either the HTML::Mason or HTML::Mason::ApacheHandler modules.
That may change which directories CGI tries to use.
You could also try

$CGI::TempFile::TMPDIRECTORY = '/tmp';

during startup, either in your httpd.conf or handler.pl
The root of the problem is probably that the temp directory is being chosen when the module loads uring server startup while its still root. It sees it can write to /usr/tmp and is happy. Then when actually running as nobody it dies.
I bet Lincoln would welcome a patch (hint, hint). One solution would be to check if you're running under mod_perl and you're root. If so, then check Apache->server->uid and see if that id can write to the temp directory too.

两种方法中,当然使用mod_perl为佳,cgi的实现方式还需要写缓存文件,如果磁盘空间不足,则会出现无法上传的情况。

一个使用的实例:http://passport.maxthon.cn/new/myprofile/avatar.html

引自:http://www.masonhq.com/?FAQ:HTTPAndHTML#h-how_can_i_handle_file_uploads_under_mason

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值