如何检查表单是否包含文件输入?ColdFusion的

De *_* De 1 coldfusion session file contain coldfusion-10

将文件上传到表单时遇到问题.基本上,我想检查会话是否包含先前在form.cfm中提交的文件.但是,遇到的问题:复杂对象类型无法转换为简单值.这些是我的代码:

form.cfm代码::

<cfif structKeyExists(form, "Submit")>

    <cfif isDefined("Form.filecontent") AND evaluate("Form.filecontent") NEQ "" > 

        <cffile action = "upload" 
            fileField = "file" 
            destination = "#GetTempDirectory()#"
            nameConflict = "overwrite">

            <cfset form.storage = cffile>

    <cfelse>

        <cfset form.storage = "">

    </cfif>

    <cfset session.checkout.input = {
        storage=form.storage
    }>

    <cflocation url="formcomplete.cfm" addToken="false">

</cfif>

<!DOCTYPE html>
<html>
<head>
    <title>Testing</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
    <!-- Other Scripts or CSS... -->
</head>
<body>
    <form method="post" enctype="multipart/form-data">

        <cfoutput>
            <input id="filecontent" name="filecontent" class="input-file" type="file" accept="application/vnd.ms-excel">
        </cfoutput>

        <button value="Submit" type="submit" id="Submit" name="Submit" class="btn btn-default">Submit</button>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

该代码显示将文件上载到临时目录并将变量保存到会话中.如果为空,则会话变量的form.storage将保存为空字符串.

formcomplete.cfm代码::

<cfif not structKeyExists(session, "checkout")>
    <cflocation url="form.cfm" addToken="false">
</cfif>

<cfif evaluate("session.checkout.input.storage") NEQ "">     <!-- !PROBLEM HERE! -->
    <cfset strPath = session.checkout.input.storage />
</cfif>
...More Codes after these.
Run Code Online (Sandbox Code Playgroud)

如果form.cfm包含该文件,则在formcomplete.cfm的第5行发生错误.如果form.cfm不包含该文件,则该过程将继续进行而不会出现问题.

错误例外::

复杂对象类型无法转换为简单值.

表达式已请求变量或中间表达式结果作为简单值.但是,结果无法转换为简单值.简单值是字符串,数字,布尔值和日期/时间值.查询,数组和COM对象是复杂值的示例.导致错误的最可能原因是您尝试将复杂值用作简单值.例如,您尝试在cfif标记中使用查询变量.

如何改进当前代码并解决问题?*更新:什么是正确的cfif语句?

Kri*_*smo 6

一些问题 - 在你的cffile中确保你得到fileField的名称正确(fileContent是你表单中字段的名称).

其次,您收到的错误是因为您没有指定cffile.serverDirectory.如果您在错误之前转储formComplete.cfm上的会话,您将看到一个结构 - 这是您的错误的原因.您需要深入了解该结构 - 特别是serverDirectory.以下是变化......

<cffile action = "upload" 
        fileField = "filecontent" 
        destination = "#GetTempDirectory()#"
        nameConflict = "overwrite">

        <cfset form.storage = cffile.serverDirectory>
Run Code Online (Sandbox Code Playgroud)

作为旁注,不需要使用evaluate函数.我会像这样重写它:

<cfif isDefined("Form.filecontent") AND Form.filecontent IS NOT "" > 
Run Code Online (Sandbox Code Playgroud)

要么

<cfif isDefined("Form.filecontent") AND len(Form.filecontent) > 
Run Code Online (Sandbox Code Playgroud)