fork download
  1. # Router.
  2. $if_admin->post('/attachment/:page_id/add')->name('on_attachment_add')
  3. ->to('attachment#on_add');
  4.  
  5. # Client side (Mojolicious template).
  6. <%= form_for 'on_attachment_add' => { page_id => $self->stash('id') }
  7. => (method => 'POST') => (enctype => "multipart/form-data")
  8. => begin %>
  9. <input type="file" name="image_upload" accept="image/*">
  10. % end
  11.  
  12. # Controller (on_add)
  13. my $self = shift;
  14. my $image_file = $self->req->upload('image_upload');
  15. if (!$image_file || $image_file->slurp eq "" || !$image_file->headers ||
  16. !$image_file->headers->content_type ||
  17. !($image_file->headers->content_type =~ /image/)) {
  18. $self->render(
  19. template => 'validation/invalid_data',
  20. status => 400
  21. );
  22. }
  23.  
  24. # test.
  25. $t->post_ok('/attachment/test_page/add' => form => {
  26. image_upload => { content => 'aaa', filename => 'x.png' },
  27. image_name => 'new_image.jpg'
  28. })->status_is(200);
Runtime error #stdin #stdout #stderr 0s 19968KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
syntax error at prog.pl line 2, near "<%= form_for 'on_attachment_add' => { "
Can't redeclare "my" in "my" at prog.pl line 10, at end of line
Execution of prog.pl aborted due to compilation errors.