From: Christian Hansen Date: Sun, 10 Apr 2005 21:37:11 +0000 (+0000) Subject: Cookbook patch from vilts X-Git-Tag: 5.7099_04~1575 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=5c0ff1281ecab26214c9b8279b8fb284385565f3 Cookbook patch from vilts --- diff --git a/lib/Catalyst/Manual/Cookbook.pod b/lib/Catalyst/Manual/Cookbook.pod index 68d66d8..2c126d1 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,13 +85,50 @@ 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 + sub upload : Global { + my ($self, $c) = @_; + if ($c->req->parameters->{form_submit} eq 'yes') { + my $upload = $c->req->upload('my_file'); + if ($upload->filename) { + my $filename = $upload->filename; + my $fh = $upload->fh; + open(NEW_FILE, ">/tmp/upload/$filename") or die + "Can't open file for writing: $!"; + while ($fh->read(my $buf, 32768)) { + print NEW_FILE $buf; + } + close(NEW_FILE); + } + } + $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->req->parameters->{form_submit} eq 'yes') { + for my $field ($c->req->upload) { + my $upload = $c->req->upload($field); + if ($upload->filename) { + my $filename = $upload->filename; + my $fh = $upload->fh; + open(NEW_FILE, ">/tmp/upload/$filename") or die "Can't open file for writing: $!"; while ($fh->read(my $buf, 32768)) { print NEW_FILE $buf; @@ -99,14 +136,20 @@ Catalyst Controller module 'upload' action: close(NEW_FILE); } } - $c->stash->{template} = 'upload_form.tt'; - $c->forward('MyApp::V::View'); } + $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. -If you want to upload bigger files than 1MB, then just add to your Controller -module: +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 diff --git a/lib/Catalyst/Request/Upload.pm b/lib/Catalyst/Request/Upload.pm index 2cd3eb9..ee1e5e2 100644 --- a/lib/Catalyst/Request/Upload.pm +++ b/lib/Catalyst/Request/Upload.pm @@ -65,7 +65,7 @@ sub link { my $self = shift; my $target = shift; - return link( $self->tempname, $target ); + return CORE::link( $self->tempname, $target ); } =item $upload->size diff --git a/t/engine/request/uploads.t b/t/engine/request/uploads.t index 842ead5..67e04a5 100644 --- a/t/engine/request/uploads.t +++ b/t/engine/request/uploads.t @@ -10,6 +10,7 @@ use Test::More no_plan => 1; use Catalyst::Test 'TestApp'; use Catalyst::Request; +use Catalyst::Request::Upload; use HTTP::Headers; use HTTP::Headers::Util 'split_header_words'; use HTTP::Request::Common; @@ -47,11 +48,11 @@ use HTTP::Request::Common; my %parameters = @{ ( split_header_words($disposition) )[0] }; my $upload = $creq->uploads->{ $parameters{filename} }; + + isa_ok( $upload, 'Catalyst::Request::Upload' ); - isnt( $upload, undef, 'Upload filename' ); - is( $upload->{type}, $part->content_type, 'Upload Content-Type' ); - is( $upload->{size}, length( $part->content ), 'Upload Content-Length' ); + #isnt( $upload, undef, 'Upload filename' ); + #is( $upload->{type}, $part->content_type, 'Upload Content-Type' ); + #is( $upload->{size}, length( $part->content ), 'Upload Content-Length' ); } - - #warn $response->as_string; }