如何检查表单是否包含文件输入?冷聚变
How to check whether form contains file input? ColdFusion
我在将文件上传到表单时遇到问题。基本上,我想检查会话是否包含先前在 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>
代码显示了将文件上传到临时目录并将变量保存到会话中。如果为空,会话变量的 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 不包含该文件,则该过程将顺利进行。
错误异常::
Complex object types cannot be converted to simple values.
The expression has requested a variable or an intermediate expression
result as a simple value. However, the result cannot be converted to a
simple value. Simple values are strings, numbers, boolean values, and
date/time values. Queries, arrays, and COM objects are examples of
complex values. The most likely cause of the error is that you tried
to use a complex value as a simple one. For example, you tried to use
a query variable in a cfif tag.
如何改进当前代码并解决问题?
*更新:正确的 cfif 语句是什么?
几个问题 - 确保在您的 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) >
我在将文件上传到表单时遇到问题。基本上,我想检查会话是否包含先前在 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>
代码显示了将文件上传到临时目录并将变量保存到会话中。如果为空,会话变量的 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 不包含该文件,则该过程将顺利进行。
错误异常::
Complex object types cannot be converted to simple values.
The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.
如何改进当前代码并解决问题? *更新:正确的 cfif 语句是什么?
几个问题 - 确保在您的 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) >