Copy link to clipboard
Copied
I'm having a real headache with trying to implement a function which will take a file from form and then upload it (or really copy it to a destination dir) using cffile.
Here is my code:
<cffunction name="Upload" access="remote" output="false" returntype="any">
<cfargument name="file" required="true">
<cffile action="upload" fileField="arguments.file" destination="#ExpandPath('.')#" nameConflict="makeunique">
<cfreturn cffile>
</cffunction>
<cfif NOT isDefined("form.submitted")>
<form action="index.cfm" method="post" enctype="multipart/form-data">
<input type="hidden" name="submitted" id="submitted" />
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
<cfelse>
<cfif isDefined("form.file")>
<cfdump var="#form#">
<cfscript>
response = Upload(Form.file);
</cfscript>
<cfdump var="#response#">
<cfoutput>Success</cfoutput>
<cfelse>
<cfoutput>No file</cfoutput>
</cfif>
</cfif>
The error message I'm getting is:
The form field arguments.file did not contain a file.
What I don't understand is, why does it work when I pass the form.file to the fileField of cffile upload and not when I simply pass it through a function? Is there some sort of hidden security here or something?
I've tried all sorts of variations with this without success. If someone could tell me where I am going wrong I would greatly appreciate it!
Thanks,
eb_dev
Ah, I just noticed what you're passing INTO that function. CFFILE takes the NAME of the form field that the file is in, not the
contents of the form field. So you should be calling it thus:
Upload(file="file");
It might be an idea if you read the docs:
cffile action = "upload"
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-738f.html
--
Adam
Copy link to clipboard
Copied
You're not giving CFFILE the name for the form field, you're just giving it the string "arguments.file".
--
Adam
Copy link to clipboard
Copied
Hi Adam, thanks for your speedy reply.
I thought that I was passing the Form.file variable as an argument 'file' to the function?
If I'm not doing this correctly could you show me the correct way to do it please?
Thanks
Copy link to clipboard
Copied
The form field name is the VALUE of the arguments.file variable. So you want to be passing the VALUE of the arguments.file variable, not simply the string "arguments.value".
So you want "#arguments.file#", not just "arguments.file".
Do you see what I mean?
--
Adam
Copy link to clipboard
Copied
Yeah I do understand that and I've also tried that as well, unfortunately it produces the following error:
The form field C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp50479.tmp did not contain a file.
Which to me says that the upload function won't accept a value unless it's come from Form. It seems passing the form.file value through a function stops it from satisfying the requirements for cffile upload. Unless I'm missing something else that is?
Copy link to clipboard
Copied
Ah, I just noticed what you're passing INTO that function. CFFILE takes the NAME of the form field that the file is in, not the
contents of the form field. So you should be calling it thus:
Upload(file="file");
It might be an idea if you read the docs:
cffile action = "upload"
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-738f.html
--
Adam
Copy link to clipboard
Copied
Right I see now, I've definitely been a bit confused about this depsite reading the docs as per usual, thanks for the solution that's working great now.
Copy link to clipboard
Copied
Right I see now, I've definitely been a bit confused about this depsite reading the docs as per usual, thanks for the solution that's working great now.
I don't think the way CFFILE uploads works is particularly intuitive, and the CF docs are a bit unhelpful, generally, so I'm not surprised it took a bit to get it working. The first time I needed to do what you were doing, it went about the same way as it did for you.
--
Adam