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

1

5

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

Quadrinomial answered 22/6, 2012 at 13:56 Comment(0)
M
7

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.
Moiramoirai answered 22/6, 2012 at 13:59 Comment(3)
Seybsen, thanks for your help! I've changed the code to that and I still get the same error on the <cffile> upload line. When that didn't work, I added "gt 0" to the if statement, but no difference. Interesting about that Form[] part. Thanks also for the tip about Evaluate().Quadrinomial
Well, it actually DOES work. I had to take everything out of my action page and just use your code and voila! I'm adding back the rest of my code and haven't found the issue yet. Sorry about that - with it giving me the same error as before, I didn't think there was something else wrong. Thanks again!! =)Quadrinomial
I found the error - I was trying to do some cfscript validation and one line was messing things up...not sure why that let it get through the if statement though. Thanks again!Quadrinomial

© 2022 - 2024 — McMap. All rights reserved.