Search code examples
coldfusionuploadcffile

How to access the object created by a cffile upload in coldfusion


I have an assortment of issues, but I'm going to concentrate on one here. How to I access the object created from a cffile upload. I am currently doing as so.

<cffile action="upload" destination="#Application.filePath#Pics/" filefield="image1" nameconflict="makeunique">
<cfif isDefined ("cffile.serverFile")>
<cfset image1Place = #cffile.serverFile#> 
</cfif>

but that doesn't seem like it would work well with multiple file uploads, which happens to be my case.


Solution

  • If you're worried about the result object being blown away as a consequence of multiple invocations of cffile, then you can use the "result" attribute to distinguish them:

    <cfset uploadResults = {}>
    <cfloop list="#form.filelist#" index="myFile">
      <cffile action="upload" destination="#Application.filePath#Pics/"
        filefield="#myFile#" nameconflict="makeunique" 
        result='uploadResults.#myFile#'>
    
      <cfif StructKeyExists(uploadResults, myFile)>
        <cfset image1Place = uploadResults[myFile].serverFile> 
      </cfif>
    </cfloop>