In the hopes of sparing another unfortunate soul from suffering the wrestling match I've been having this week...<div><br></div><div><br></div><div>SYMPTOM: Regardless of how many upload-file fields you have in your form, Catalyst doesn't see the file uploads. That is, <b>$c->req->uploads</b> is an empty hash?! And <b>$c->req->upload</b> is an empty array?! File uploads are not working at all!</div>
<div><br></div><div>Let's say you develop a form that works just fine, and then you add an upload field. Everything else still works as before, but the uploads are ignored: <b>$c->req->uploads</b> is an empty hash (and <b>$c->req->upload</b> is an empty array). You can see some of the details in <b>$c->req->_body</b> but there's no uploaded info to work with! You can't get the uploaded file from /tmp/blahblahblah because there isn't any such uploaded file. You can't see the size or the content or the mime-type. It's as if nothing is uploaded at all.<br>
<div><br></div><div><br></div><div>SOLUTION: To deal with uploaded files, your form must use "<b>multipart/form-data</b>"! It's not Catalyst's fault, your web browser behaves differently depending on the <form> attributes you specified. Here's the primary snippet to add when using HTML::FormHandler...</div>
<div><br></div><div>{</div><div> package MyApp::Form::UploadForm;</div><div><div> use HTML::FormHandler::Moose;</div><div> extends 'HTML::FormHandler::Model::DBIC';</div><div> with 'HTML::FormHandler::Render::Table';</div>
</div><div><br></div><div><meta charset="utf-8"><div><div> has '+item_class' => ( default => 'RecordSpecHere' );</div></div></div><div><br></div><div><div><span class="Apple-style-span" style="background-color: rgb(255, 255, 153);"> # MULTIPART/FORM-DATA is MANDATORY for file-uploads, beware!</span></div>
<div><span class="Apple-style-span" style="background-color: rgb(255, 255, 153);"><b> has '+enctype' => ( default => 'multipart/form-data' );</b></span></div><div><div><br></div><div><div> has_field 'id' => ( type => 'Hidden', );</div>
</div></div><div>#...<br> has_field 'myfile' => ( type => 'Upload' );<br>}<br><br></div><div><br></div><div>At any rate, your form tag will now look a bit like this:</div><div><br></div><div><form enctype="<b>multipart/form-data</b>" method="<b>post</b>" id="form996"></div>
<div><br></div><div>Single-stepping through the Perl debugger will now show that Catalyst has everything you need in <b>$c->req->uploads</b>:</div><div><br></div><div><div>DB<3> <b>x $c->req->uploads</b></div>
<div>0 HASH(0xc54f598)</div><div> 'myfile' => Catalyst::Request::Upload=HASH(0xc56d858)</div><div> 'filename' => 'sample-file.txt'</div><div> 'headers' => HTTP::Headers=HASH(0xc56d918)</div>
<div> 'content-disposition' => 'form-data; name="myfile"; filename="sample-file.txt"'</div><div> 'content-type' => 'application/octet-stream'</div>
<div> 'size' => 74</div><div> 'tempname' => '/tmp/BqoVhCmj3f'</div><div> 'type' => 'text/plain'</div></div><div><br></div><div>Woo hoo!</div><div><br></div><div>
<br></div>-- <br>Failure is not important. How you overcome it, is.<br>-- Nick Vujicic<br>
</div></div>