Advertisement
Guest User

Mojolicious + fileuploader.js server side example

a guest
Mar 6th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.91 KB | None | 0 0
  1. # Mojolicious + fileuploader.js (http://valums.com/ajax-upload/)
  2. # Example for server side, compatible with IE6 ;)
  3.  
  4. sub upload {
  5.     my $self = shift;
  6.  
  7.     my %res = ();
  8.  
  9.     if ($self->req->upload('qqfile') || $self->req->body) {
  10.         my $fh = File::Temp->new(
  11.             TEMPLATE => 'X' x 12,
  12.             UNLINK   => 0,
  13.             SUFFIX   => '_' . time . '_orig.jpg',
  14.             DIR      => $self->app->home->rel_dir('public/fotos'),
  15.         );
  16.  
  17.         if ($fh) {
  18.             if ($self->req->upload('qqfile')) {
  19.                 close $fh;
  20.                 $self->req->upload('qqfile')->move_to($fh->filename);
  21.             }
  22.             else {
  23.                 print $fh $self->req->body;
  24.                 close $fh;
  25.             }
  26.  
  27.             $res{success} = 'true';
  28.             $fh = undef;
  29.         }
  30.     }
  31.  
  32.     my $json = Mojo::JSON->new;
  33.     $self->render({text => $json->encode(\%res)});
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement