【问题标题】:How to check whether form contains file input? ColdFusion如何检查表单是否包含文件输入?冷融合
【发布时间】:2015-01-05 09:02:22
【问题描述】:

我在将文件上传到表单时遇到问题。基本上,我想检查 session 是否包含之前在 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>

代码显示了将文件上传到临时目录并将变量保存到会话。如果为空,则 session 变量的 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.

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

错误异常::

复杂对象类型不能转换为简单值。

表达式请求了一个变量或一个中间表达式 结果作为一个简单的值。但是,结果不能转换为 简单的价值。简单值是字符串、数字、布尔值和 日期/时间值。查询、数组和 COM 对象是 复杂的价值观。错误的最可能原因是您尝试过 将复杂值用作简单值。例如,您尝试使用 cfif 标记中的查询变量。

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

【问题讨论】:

    标签: file session coldfusion coldfusion-10 contain


    【解决方案1】:

    几个问题 - 确保在您的 cffile 中获得正确的 fileField 名称(fileContent 是表单中的字段名称)。

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

    <cffile action = "upload" 
            fileField = "filecontent" 
            destination = "#GetTempDirectory()#"
            nameConflict = "overwrite">
    
            <cfset form.storage = cffile.serverDirectory>
    

    附带说明一下,没有必要使用评估函数。我会这样重写它:

    <cfif isDefined("Form.filecontent") AND Form.filecontent IS NOT "" > 
    

    <cfif isDefined("Form.filecontent") AND len(Form.filecontent) > 
    

    【讨论】:

    • RE:特别是 serverDirectory 虽然要获得完整路径,您需要同时使用 both serverDirectoryserverFilevariables。
    猜你喜欢
    • 2011-07-05
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多