Stetec has asked for the wisdom of the Perl Monks concerning the following question:

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="multi +part/form-data" method=post> <input type="checkbox" id="submited_chck" name="submited" +checked hidden> <input type="file" name="attached_files" id="file_upload_b +tn" 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? I posted this also on stack overflow but no-one is able to help. Here is a link to it https://stackoverflow.com/questions/48290475/getting-attachment-from-request-in-perl

Replies are listed 'Best First'.
Re: Getting attachments from request in perl
by Stetec (Novice) on Jan 19, 2018 at 13:03 UTC
    I just had to change:  my $req = new CGI; to:  my $req = $m->cgi_object; and now it works perfectly. Thank you for your time.
Re: Getting attachments from request in perl
by Anonymous Monk on Jan 18, 2018 at 15:31 UTC
    StackOverflow has a good thread on exactly how HTTP file-uploads work. Are you sure that that unless (@attachments) business is correct? Can you, say, put a Data::Dumper output to a temporary file, to see what it contains?
      When I inspect my requests in Mozilla the files are present in each one correctly. The  unless statement should be correct. I also tried  if (scalar @attachments == 0) with the same results. I don't exactly know how should I use Data::Dumper but when I do the first request after restart the file is present and I can open and copy its content normally.
        I don't exactly know how should I use Data::Dumper

        Best to have a quick look at the Data::Dumper documentation. The Dumper() function is exported by default and gives a pretty crude picture of the data. Give it an array and it will treat each element as a separate variable but give it a reference to the array and you see an anonymous array. Dump() and its faster but not always available sibling Dumpxs() are fiddlier but more useful as they can give a more accurate picture.

        Here's a brief example.

        johngg@shiraz ~ $ perl -Mstrict -Mwarnings -MData::Dumper -E ' my @atts = qw{ att1 att2 att3 }; say Dumper( @atts ); say Dumper( \ @atts ); say Data::Dumper->Dump( [ \ @atts ], [ qw{ *atts } ] ); say Data::Dumper->Dumpxs( [ \ @atts ], [ qw{ *atts } ] );' $VAR1 = 'att1'; $VAR2 = 'att2'; $VAR3 = 'att3'; $VAR1 = [ 'att1', 'att2', 'att3' ]; @atts = ( 'att1', 'att2', 'att3' ); @atts = ( 'att1', 'att2', 'att3' );

        I hope this is helpful.

        Cheers,

        JohnGG