I'm facing a problem when uploading a file to the form. Basically, I want to check whether the session contains the file submitted previously in form.cfm. However, problems encountered: Complex object types cannot be converted to simple values. These are my codes:
form.cfm code ::
<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>
The code shows the upload of file to temp directory and save variable to the session. If empty, form.storage of session variable will be saved as empty string.
formcomplete.cfm code ::
<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.
If form.cfm contains the file, error occurs at 5th lines of formcomplete.cfm. If form.cfm does not contain the file, the process will proceed without problems.
Error Exception ::
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.
How can I improve on the current code and solve the problem? *Update: What is the correct cfif statement?
A couple problems - make sure in your cffile that you get the name of the fileField correct (fileContent is the name of the field in your form).
Secondly, the error you're receiving is because you didn't specify cffile.serverDirectory. If you dump the session on formComplete.cfm before your error you'll see a struct - that's the cause of your error. You needed to go a level deeper into that struct - specifically the serverDirectory. Here are the changes...
<cffile action = "upload"
fileField = "filecontent"
destination = "#GetTempDirectory()#"
nameConflict = "overwrite">
<cfset form.storage = cffile.serverDirectory>
And as a side note, the use of the evaluate function is not necessary. I would rewrite it like this:
<cfif isDefined("Form.filecontent") AND Form.filecontent IS NOT "" >
or
<cfif isDefined("Form.filecontent") AND len(Form.filecontent) >