file-uploadcoldfusioncfccffilecffunction

Coldfusion FileUpload function from cfc file not working


I am currently trying to upload a file using a function from a cfc file. I can get the code to work when I just call the <cffile action="upload"> on the same page. However, I wanted to learn how to call it from a function in a cfc file. I believe there is a problem with the arguments I'm passing to the attributes of the <cffile> tag, but I'm not sure.

Here is my HTML form:

<form action="confirm.cfm" method="POST" enctype="multipart/form-data">
    First Name: <input type="text" name="FNAME" size="25" /> <br />
    Last Name: <input type="text" name="LNAME" size="30" /> <br />
    Upload Attachment File Here: <input type="file" name="fileUpload" 
                                    id="fileUpload" size="30" 
                                    onchange="PassFileName()" /> <br />

    <input type="hidden" name="fileName" id="fileName" /> <br />
    <input type="submit" value="submit" />
    <input type="reset" name="Reset Form" />
    <div id="filenamesection">
    </div>
</form> 

Here is the code that the form is submitting to the confirm.cfm page

<cfif isDefined("Form.fileUpload")>
    <cfinvoke component="FileUploadExample"
            method="UploadFile"
            fileToUpload="#Form.fileUpload#"
            sizeMax="50"
            returnvariable="FileNameReturn">
    <cfelse>
            <cflocation url="index.cfm">
    </cfif>
    <h1> Thank for filling out this form </h1>
    <cfoutput>
    <table>
    <h3> Please review and confirm the information below </h3>
    <tr>
        <td> is this the first name you entered </td>
        <td>  #Form.FNAME# </td>
    </tr>
    <tr>
        <td> is this the last name you entered </td>
        <td>  #Form.LNAME# </td>
    </tr>
    <tr>
        <td> is this the name of the file you uploaded? </td>
        <td>  #Form.fileName# </td>
    </tr>
    </table>
    </cfoutput>

    The File <cfoutput> #Form.fileName# </cfoutput> has been uploaded 

Here is the code from my FileUploadExample.cfc file

<cfcomponent>
    <cffunction name="uploadFile" access="public" output="no" returntype="string">
        <cfargument name="fileToUpload" type="string" required="no">

        <cfset var cffile="">
        <cffile action="upload"
                destination="C:\filepath\example\whatevaFolder\"
                filefield="#ARGUMENTS.fileToUpload#"
                nameconflict="overwrite">
        <cfreturn cffile.clientFile>
    </cffunction>
</cfcomponent>

The error I keep receiving when I upload a file is:

neotmp84939430443.tmp did not contain a file.


Solution

  • When using the upload with cffile, it needs a form field name - what you are passing in is the form field value. Please see this link for the exact method to achieve what you are trying to do.


    CFFILE Upload in a CFC

    I keep seeing people get tripped up over this, so here's what the deal is. The biggest thing you have to remember about cffile's upload is that no matter what, the filefield has to be the name of the form field that you're trying to upload.

    Example:

    Note the lack of pound signs. That means that you're forcing cffile to go look for form.something and grab the data stream out of it. So, what do you have to do if you put it in a CFC? You can't pass the datastream into a CFC (you can, but that's not the correct way to do it). You have to pass the name of the file field into the cfc instead.

    Example CFC:

    <cfcomponent name="fileio">
      <cffunction name="fileUpload" access="public" returntype="struct">
        <cfargument name="fileField" required="true" type="string">
        <cfargument name="destination" required="true" type="string">
    
        <cffile action="upload" filefield="#arguments.fileField#" destination="#arguments.destination#">
      </cffunction>
    </cfcomponent>
    

    Not the pound signs around arguments.fileField. Why? Because you're evaluating the value of arguments.fileField which is actually a string: 'form.something'. Example usage would be:

    <cfscript>
      fileObj = createObject('component','fileio');
      fileObj.fileUpload('form.something','c:\');
    </cfscript>