I'm trying to do an exception for cffile upload to ensure users only upload xls-related documents or no upload at all. I have a issue with the file upload. Even though the users may not attach anything to the input, the form will still go through and assume the form field b1b3formAttach as defined. This is the code:
<cfif structKeyExists(form, "Submit")>
<cfif isDefined("Form.b1b3formAttach") >
<cffile action = "upload"
fileField = "b1b3formAttach"
destination = "#GetTempDirectory()#"
nameConflict = "overwrite">
<cfif isDefined("CFFILE.serverFile")>
<cfset session.slot = cffile.serverFile>
<cfelse>
<cfset form.storage = "">
</cfif>
</cfif>
<body>
<label class="control-label" for="b1b3formAttach">Attach the completed B1/B3 Form*</label></br>
<div class="controls">
<input id="b1b3formAttach" name="b1b3formAttach" class="input-file" type="file">
</div>
<div class="controls">
<button value="Submit" type="submit" id="Submit" name="Submit" class="btn btn-default">Submit</button>
</div>
</body>
Brief description of error:
The form field b1b3formAttach did not contain a file.
The error occurred in C:/ColdFusion11/cfusion/wwwroot/wwwroot/form.cfm: line 21
19 : fileField = "b1b3formAttach"
20 : destination = "#GetTempDirectory()#"
21 : nameConflict = "overwrite">
22 : <cfif isDefined("CFFILE.serverFile")>
23 : <cfset form.storage = cffile.serverFile>
What is the ideal way to validate whether the input contains a file?
Going through your code, here's what I did to make it work. I've made comments inside to explain some of the changes:
Hopefully this can help you get on the way.