我尝试对cffile上传执行例外操作,以确保用户只上传xls相关文档或根本不上传。 我有文件上传的问题。 即使用户可能没有将任何内容附加到输入,表单仍将通过并假定表单字段b1b3formAttach定义。这是代码:
<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>
错误简要说明:
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>
验证输入是否包含文件的理想方法是什么?
答案 0 :(得分:5)
仔细阅读您的代码,这就是我所做的工作。我在里面做了一些评论来解释一些变化:
<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>
希望这可以帮助你顺利上路。