Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFFILE.SERVERFILE 1

Status
Not open for further replies.

dougcranston

Technical User
Oct 5, 2001
326
US
Ladies and Gentlemen,

I hope someone can possibly provide some direction or help on this.

Trying to create a simple file upload with a CFC doing the processing.

Simple business rules.

1) Restrict the file type to Word

2) Upload one file (not multiples)

3) Assume the user will have Word files named with unacceptable special characters .. spaces, #, ! etc.

4) Ultimately plan to capture additional information along with the renamed filename into an SQL Server database to present the user an option to click n view on a page. (that is after I get past this.)

I had created a simple couple .cfm's that worked, but wanted to move it to a .CFC and invoke it from the action page. I thought it would be a simple process, however, that has not been the case.

My attempts failed. So I started doing some Googling.

In the process I stumbled on a snippet of code associated with Farcry. It appeared to do alot of what I would like to achieve, but as this was my first foray into a serious CFC I wanted to follow a piece of working code to step through and follow.

My attempt succeeds in uploading the file, but ERRORs at the point where I attempt to identify the filename (using makeunique) to ensure it is unique.

I have searched the web, and nothing seems to address this issue.

I have checked all the CF books I have and have not had any success.

As noted above the app fails after successfully uploading and saving the file to the destination folder.

The error "Element SERVERFILE is undefined in CFFILE." implies to me that it recognizes the CFFILE object but not the SERVERFILE. I have tried lowercase, uppercase, camel case. I have tried both FILE vs CFFILE upper and lower case. Same result.

My swag on this is it almost seems like the serverfile is referencing the .tmp file that has been moved to the DESTINATION folder and the undefined is a result of nothing there. Or that I am using incorrect but verified the FILE Attributes in CF MX & Web Application Construction Kit, pg 1024 and again on the Adobe site.

It appears from the research that the file is really uploaded to ...\ directory

I am testing this on a WinXP Pro running PWS and developers edition of Coldfusion MX7

CF version 7.0.0.91690


My folder structure is:
/fload
/fload/docs


My two CFM and the CFC files are as follows.

I really suspect it is a SUP problem.. Stupid User Problem.

Any suggestions or ideas would be greatly appreciated.

Thanks in advance

DougC



<<<<<frmupload.cfm>>>>>
======================
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="upload_act.cfm" method="post" enctype="multipart/form-data">
File: <input type="file" name="aFile"><br>
<input type="submit" value="Upload a File">
</form>
</body>
</html>


<<<<<upload_act.cfm>>>>>
======================
<cfdump var="#form#">
<cfdump var="#form.afile#">

<cfinvoke
component="uploadfileNew"
method="uploadFile"
returnvariable="uploadFooRet">
<cfinvokeargument name="formField" value="form.aFile">
<cfinvokeargument name="destination" value="c:\inetpub\ <cfinvokeargument name="nameconflict" value="makeunique">
<cfinvokeargument name="accept" value="application/msword">
</cfinvoke>


<<<<uploadfileNew.cfc>>>>>
======================
<cfcomponent displayName="Upload File">
<!--- Function to address uploading a file, and then renaming the file to strip spaces and other
special characters out of the filename Found on --->

<cffunction name="uploadFile" hint="Uploads a file">
<cfargument name="formField" hint="The name of the field that contains the file to be uploaded" required="true" type="string">
<cfargument name="destination" hint="Directory file is to be uploaded to - must pass in absolute path" type="string" default="">
<cfargument name="nameconflict" hint="File write behavior" type="string" default="">
<cfargument name="accept" hint="File types to accept" type="string" default="">

<cfset var stReturn = structNew()>
<cfset var validName = ''>
<cfset var i = 1>
<cfset stReturn.bSuccess = false>

<cfif len(arguments.formField)>
<cftry>
<cfif NOT directoryExists(arguments.destination)>
<cfdirectory action="create" directory="#arguments.destination#">
</cfif>

<cffile
action="UPLOAD"
filefield="#arguments.formField#"
destination="#arguments.destination#"
nameconflict="#arguments.nameconflict#"
accept="#arguments.accept#"
attributes = "normal"
result = "result_name">
<!--- Fails from this point on.. File uploads but get following error:
Exceptions

15:58:48.048 - Expression Exception - in C:\Inetpub\ : line 36
Element SERVERFILE is undefined in CFFILE. This is the following <cfif statement
--->
<cfif refindnocase("[\$\^\s\%\*''""<>,\&?]",cffile.serverfile) gt 0>
<cfset validName = rereplace(cffile.serverfile,"[?\$\^\s\%\*''""<>,\&]","_","ALL")>

<cfset i = 1>
<cfloop condition="#fileexists('#cffile.ServerDirectory#/#validName#')#">
<cfset validName = rereplace(cffile.clientfilename,"[?\$\^\s\%\*''""<>,\&]","_","ALL") & "#i#." & listlast(cffile.serverfile,".")>
<cfset i = i + 1>
</cfloop>

<cffile
action="rename"
source="#cffile.ServerDirectory#/#cffile.serverfile#"
destination="#cffile.ServerDirectory#/#validName#">
<cfelse>
<cfset validName = cffile.serverfile>
</cfif>

<cfscript>
stReturn.bSuccess = true;
stReturn.message = "File upload Successful";
stReturn.filename = validName;
stReturn.fileDirectory = cffile.ServerDirectory;
stReturn.fileSize = cffile.fileSize;
stReturn.contentType = cffile.ContentType;
stReturn.clientFileName = cffile.clientFileName;
stReturn.contentSubType = cffile.contentSubType;
stReturn.serverFile = validName;
stReturn.serverDirectory = cffile.ServerDirectory;
</cfscript>

<cfcatch>
<cfset stReturn.message = cfcatch.message>
</cfcatch>
</cftry>
<cfelse>
<cfset stReturn.message = "No file uploaded.">
</cfif>
<cfreturn stReturn>
</cffunction>

</cfcomponent>
 
imstillatwork,

Thanks for the suggestion.

Tried that and I get:

struct
AFILE C:\JRun4\servers\uat\SERVER-INF\temp\cfusion.war-tmp\neotmp51021.tmp
FIELDNAMES AFILE
C:\JRun4\servers\uat\SERVER-INF\temp\cfusion.war-tmp\neotmp51021.tmp

struct [empty]
: from within cfc

struct [empty]

The last struct [empty] coincides with <cfdump added at the end of the upload_act.cfm

The struct [empty] : from within cfc coincides with <cfdump added at the end of the uploadfileNew.cfc

This call was IMMEDIATELY after the <cffile action="UPLOAD"

So any additional thoughts?

Thanks in advance.

Dougc
 
LOL....

I copied your code to my server and ran it and it worked, the file uploaded, but as you said, no cffile info...

I can't believe I missed this just from reading your posts!

if you use the result attribute of cffile, the cffile scope does not exist. you have to call it as result_name!

everything else works just fine.

So the QUICKEST fix would be to remove the result attribute and just use the standard cffile scope, which is already coded in the script.

unless you have multiple cffile upoads, you don't need the result attribute over the cffile scope.

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
imamstillatwork,

Figured out my problem.. Was a SUP.

<cffile
action="UPLOAD"
filefield="#arguments.formField#"
destination="#arguments.destination#"
nameconflict="#arguments.nameconflict#"
accept="#arguments.accept#"
attributes = "normal"
result = "result_name">

It was the last line [result = "result_name">] that caused the problem.

Notes in the books stated that cffile was the default but it was not til I took the advice of one of my friends to do a <cfdump var="#variables#> that an inkling of the shot in the foot had a occurred. Searching Livedocs found:

result
Optional

Allows you to specify a name for the variable in which cffile returns the result (or status) parameters. If you do not specify a value for this attribute, cffile uses the prefix 'cffile'. For more information, see the Usage section.

Thanks again for your response, and have a great day.

Dougc
 
Kevin,

Ok you get the star.. Your second response just dropped in through the firewall...

When I sent my last note I just started typing and failed to read your post..

Thanks for the help...

Dougc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top