Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion - How to check if upload filefield is empty - esp with dynamic field name?

I am giving the form a dynamic number of file upload fields. So on the form side, I'm looping through this dynamic number (say 3, as index 'm'), and naming each filefield input 'ResumeFile#m#'. On the action page, I'm getting an error if one of the filefields is not filled in. When all 3 filefields are filled in, it works fine.

The error is (here, when filefield 2 is not filled in):

    The form field Form.ResumeFile2 did not contain a file. 

Here's my code creating the form:

    <cfset numUploads = 3>
    <cfform name="uploadMultipleResumes" action="uploadMultipleResumes.cfm" enctype="multipart/form-data" >
        <cfinput name="EmployeeID" type="hidden" value="#form.EmployeeID#">
        Resume File(s): *<BR>
        <cfloop from="1" to="#numUploads#" index="j">
            <cfinput name="ResumeFile#j#" type="file" size="50">
        </cfloop>
        <BR />
        <cfinput name="Submit" type="submit" value="Upload Multiple Resumes">
    </cfform>

Here's my code (abbreviated) from the action page. I've tried lots of combinations of how to check if each filefield is not filled in, all of which allow a blank input to pass through and get to the error I mentioned above.

    <!--- Loop over multiple file fields --->
    <cfloop from="1" to="#numUploads#" index="m">
        <cfif 'Form.ResumeFile#m#' is not "">
            <cffile action="upload" filefield="#Evaluate('Form.ResumeFile#m#')#" nameconflict="makeunique" destination="#destinationPath#">
        </cfif>
    </cfloop>

Even if I've already tried it, I will try all suggestions! I've been on this for days and I know there's an answer!

Thanks so much!

Here's the image of the error

Blockquote

like image 645
Kirsten Avatar asked Jun 22 '12 13:06

Kirsten


1 Answers

Change the code on your action page to the following:

<!--- Loop over multiple file fields --->
<cfloop from="1" to="#numUploads#" index="m">
    <cfif len(Form["ResumeFile#m#"])>
        <cffile action="upload" filefield="Form.ResumeFile#m#" nameconflict="makeunique" destination="#destinationPath#">
    </cfif>
</cfloop>

Fixed also some performance bottlenecks:

  1. Have in mind that one should if possible always avoid the use of evaluate().
  2. Use len() instead of IS NOT "" to check for a non empty string.
like image 179
Seybsen Avatar answered Sep 30 '22 09:09

Seybsen