perlmason

Getting attachment from request in perl


I want to use a form to submit fields of email so server can send it. I use Perl and Mason to handle this. I want a user to be able to add multiple attachments, however I am encountering a problem that I can not solve. This is my code simplified as much as possible and with added check for the attachments. ( After submit I want to stay at the same page, that is why there is a hidden check-box and condition in Init section, so that it does nothing when I visit the page for the first time. )

<!DOCTYPE html>
<html>
%foreach (@messages) {
    <div class="alert">
        <% $_ | h %>
    </div>
%}
    <body>
       <form action="/Tools/SendEmail.html" name="form" enctype="multipart/form-data" method=post>
            <input type="checkbox" id="submited_chck" name="submited" checked hidden>
            <input type="file" name="attached_files" id="file_upload_btn" multiple> 
            <input type="submit" value="Submit">
        </form> 
    </body> 
</html>

<%args>
@messages => ()
$submited => ''
</%args>

<%init>

use strict;
use warnings;
use CGI;

if($submited eq 'on') {

    my $req = new CGI;

    my @attachments = $req->param('attached_files');

    unless (@attachments) {
        push @messages, "Attachments do not exist";
    }
}

</%init>

The problem is that if I do any request before submit and add any number of attachments it does not get the attachment from request and the error message is pushed into array and displayed. It does not push the error message only if I restart apache service and submit the form right after it. Any ideas what can cause this?


Solution

  • I just had to change:

    my $req = new CGI;
    

    to:

    my $req = $m->cgi_object;
    

    and now it works perfectly.