Validation 表单字段已定义但不包含文件?冷饮

Validation 表单字段已定义但不包含文件?冷饮,validation,coldfusion,upload,coldfusion-10,cffile,Validation,Coldfusion,Upload,Coldfusion 10,Cffile,我试图为cffile上传做一个例外,以确保用户只上传xls相关的文档,或者根本不上传。 我对文件上传有问题。 即使用户可能没有将任何内容附加到输入,表单仍将按照定义执行并假定表单字段b1b3formAttach。代码如下: <cfif structKeyExists(form, "Submit")> <cfif isDefined("Form.b1b3formAttach") > <cffile action = "upload"

我试图为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>

附上填写好的B1/B3表格*
提交
错误的简要说明:

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>
表单字段b1b3formAttach不包含文件。
错误出现在C:/ColdFusion11/cfusion/wwwroot/wwwroot/form.cfm:第21行
19:fileField=“b1b3formAttach”
20:destination=“#GetTempDirectory()#”
21:nameConflict=“overwrite”>
22 :    
23 :    

验证输入是否包含文件的理想方法是什么?

仔细查看您的代码,下面是我为使其正常工作所做的工作。我在里面发表了一些评论来解释其中的一些变化:

<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>

附上填写好的B1/B3表格*

希望这能帮助您上路。

仔细阅读您的代码,下面是我为使其工作所做的。我在里面发表了一些评论来解释其中的一些变化:

<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>

附上填写好的B1/B3表格*

希望这能帮助您上路。

@DeDeDeDe-Side注意,不需要检查是否定义了
CFFILE.serverFile
。它总是在执行
action=upload
后定义。正如切斯特的评论所指出的,主要问题是缺少带有正确的
enctype
的表单标记,以及缺少
@dedededede-Side注释,不需要检查是否定义了
CFFILE.serverFile
。它总是在执行
action=upload
后定义。正如切斯特的评论所指出的,主要问题是缺少带有正确的
enctype的表单标记,以及缺少