X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FCookbook.pod;h=aa38da5c526761a9390d544596d0dac235eceef3;hb=47ae6960753acadac1da528c87c5d5009b675281;hp=68d66d8724d7101034d0a64d6b38c76daaccf87b;hpb=61b1e958102e2371a79e07a7e2cdbb371797d202;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Manual/Cookbook.pod b/lib/Catalyst/Manual/Cookbook.pod index 68d66d8..aa38da5 100644 --- a/lib/Catalyst/Manual/Cookbook.pod +++ b/lib/Catalyst/Manual/Cookbook.pod @@ -69,7 +69,7 @@ Just use Catalyst::Model::CDBI::CRUD as baseclass. 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: @@ -85,28 +85,71 @@ if it's not there, uploads just don't work. 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($target) || $upload->copy($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: + +
+ +
+
+
+ +
+ +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($target) || $upload->copy($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 and C. =head2 Authentication with Catalyst::Plugin::Authentication::CDBI