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:
<cfif structKeyExists(form, "Submit")>
<cfif isDefined("Form.b1b3formAttach") >
<cffile action = "upload"
fileField = "b1b3formAttach"
destination = "#GetTempDirectory()#"
nameConflict = "overwrite">
<!---Used to show success of upload--->
<cfdump var="#cffile#">
<cfif isDefined("CFFILE.serverFile")>
<cfset session.slot = cffile.serverFile>
<cfelse>
<cfset form.storage = "">
</cfif>
</cfif>
<!---Missing closing CFIF tag--->
</cfif>
<body>
<cfoutput>
<!---Form fields not wrapped in FORM tags; form posts to itself (cgi.script_name) include "enctype" to attach files--->
<form action="#cgi.script_name#" method="post" enctype="multipart/form-data">
<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">
<!---Changed SUBMIT BUTTON from BUTTON type to INPUT type --->
<input value="Submit" type="submit" id="Submit" name="Submit" class="btn btn-default">
</div>
</form>
</cfoutput>
</body>
Hopefully this can help you get on the way.