Modify the $c->form() parameters to match your needs, and don't forget to copy
the templates. ;)
-=head2 Uploads with Catalyst
+=head2 Single file upload with Catalyst
To implement uploads in Catalyst you need to have a HTML form similiar to
this:
Catalyst Controller module 'upload' action:
- sub upload : Global {
- my ($self, $c) = @_;
- if ($c->req->parameters->{form_submit} eq 'yes') {
- my $filename = $c->req->parameters->{my_file};
- if ($filename) {
- my $fh = $c->req->uploads->{$filename}->{fh};
- open(NEW_FILE, ">/tmp/$filename") or die
- "Can't open file for writing: $!";
- while ($fh->read(my $buf, 32768)) {
- print NEW_FILE $buf;
- }
- close(NEW_FILE);
+ sub upload : Global {
+ my ($self, $c) = @_;
+
+ if ( $c->request->parameters->{form_submit} eq 'yes' ) {
+
+ if ( my $upload = $c->request->upload('my_file') ) {
+
+ my $filename = $upload->filename;
+ my $target = "/tmp/upload/$filename";
+
+ unless ( $upload->link_to($target) || $upload->copy_to($target) ) {
+ die( "Failed to copy '$filename' to '$target': $!" );
+ }
+ }
+ }
+
+ $c->stash->{template} = 'file_upload.html';
+ }
+
+=head2 Multiple file upload with Catalyst
+
+Code for uploading multiple files from one form needs little changes compared
+to single file upload.
+
+Form goes like this:
+
+ <form action="/upload" method="post" enctype="multipart/form-data">
+ <input type="hidden" name="form_submit" value="yes">
+ <input type="file" name="file1" size="50"><br>
+ <input type="file" name="file2" size="50"><br>
+ <input type="file" name="file3" size="50"><br>
+ <input type="submit" value="Send">
+ </form>
+
+Controller:
+
+ sub upload : Local {
+ my ($self, $c) = @_;
+
+ if ( $c->request->parameters->{form_submit} eq 'yes' ) {
+
+ for my $field ( $c->req->upload ) {
+
+ my $filename = $upload->filename;
+ my $target = "/tmp/upload/$filename";
+
+ unless ( $upload->link_to($target) || $upload->copy_to($target) ) {
+ die( "Failed to copy '$filename' to '$target': $!" );
}
}
- $c->stash->{template} = 'upload_form.tt';
- $c->forward('MyApp::V::View');
}
-If you want to upload bigger files than 1MB, then just add to your Controller
-module:
+ $c->stash->{template} = 'file_upload.html';
+ }
+
+for my $field ($c->req->upload) loops automatically over all file input
+fields and gets input names. After that is basic file saving code, just like in
+single file upload.
+
+Notice: die'ing might not be what you want to do, when error occurs, but
+it works as an example. Better idea would be to store error $! in
+$c->stash->{error} and show custom error template displaying this message.
- $CGI::Simple::POST_MAX = 1048576000;
+For more information about uploads and usable methods look at
+C<Catalyst::Request::Upload> and C<Catalyst::Request>.
=head2 Authentication with Catalyst::Plugin::Authentication::CDBI
Catalyst applications give optimum performance when run under mod_perl.
However sometimes mod_perl is not an option, and running under CGI is
-just too slow. There are two alternatives to mod_perl that give
-reasonable performance: FastCGI and PersistentPerl.
+just too slow. There's also an alternatives to mod_perl that gives
+reasonable performance named FastCGI.
B<Using FastCGI>
For more information see the FastCGI documentation, the C<FCGI> module
and L<http://www.fastcgi.com/>.
-
-B<PersistentPerl>
-
-PersistentPerl (previously known as C<CGI::SpeedyCGI>) is a persistent
-Perl interpreter. After the script is initially run, instead of
-exiting, the perl interpreter is kept running. During subsequent runs,
-this interpreter is used to handle new executions instead of starting
-a new perl interpreter each time. A very fast frontend program contacts
-the persistent Perl process, which is usually already running, to do
-the work and return the results.
-PersistentPerl can be used to speed up perl CGI scripts. It also
-provides an Apache module so that scripts can be run without the
-overhead of doing a fork/exec for each request.
-
-The code for PersistentPerl is simpler than for FastCGI; rather than
-waiting in an accept loop the script runs to completion, however
-variables are not reinitialized on subsequent runs but maintain their
-values from the previous run.
-
-
- #!/usr/bin/perperl
- use strict;
- use vars qw($output $initialized);
- use PersistentPerl;
- use MyApp;
-
- if (!$initialized++) {
- # initialization code - set up database, etc
- if ($PersistentPerl::i_am_per_perl) {
- # PP-specific initialization code
- }
- }
-
- MyApp->run;
-
-For more information see the C<PersistentPerl> documentation.
-
-
=head1 AUTHOR
Sebastian Riedel, C<sri@oook.de>