Skip to main content
July 1, 2011
Answered

cffile upload - how do you pass form.file through a function?

  • July 1, 2011
  • 1 reply
  • 1715 views

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



This topic has been closed for replies.
Correct answer Adam Cameron.

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?



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

1 reply

Inspiring
July 1, 2011

You're not giving CFFILE the name for the form field, you're just giving it the string "arguments.file".

--

Adam

July 1, 2011

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

Inspiring
July 1, 2011

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