Search code examples
coldfusioncoldfusion-2021

How do you tell files apart in the output from getPartsArray?


We have some Coldfusion pages that accept files, and we're using some code on the target page to process those files, starting with this:

<cfset tmpPartsArray    = caller.form.getPartsArray() />
<cfif IsDefined("tmpPartsArray")>
    <cfloop array="#tmpPartsArray#" index="tmpPart">
        <cfif tmpPart.isFile()>
            <cfset thisFileName = tmpPart.getFileName() />
            <cfset thisExt      = listLast(thisFileName, ".")>
        </cfif>
    </cfloop>
</cfif>

This worked fine in our initial tests, but when testing on a page with multiple form file inputs it would always take the name and extension of the first one (since there's nothing in the above to make sure we've got the right file field; just that we have a file field). I need some way to make sure the tmpPart that I'm looking at is the right file.

When I dump tmpPart, I don't see any function that allows me to get the name of the input that the file came from. I can get the original file name, but that doesn't help me because there's no way for me to get the original file name out of the form scope in CF so I still can't tell which file is the right file.

The only way I can figure to find the right tmpPart is to do a cfFile upload and then take the filename from that to compare against getFileName(), but the whole point of using the PartsArray is to avoid doing a cfFile upload. (avoiding cfFile upload is a requirement that I cannot change)

Is there any way, when looping through the partsArray, to determine which file field we're looking at?


Solution

  • I realized after getting a second set of eyes on this that I was missing the functions inherited from the parent class, and could use getName() to get the fieldName of the submitted file.

                <cfset tmpPartsArray    = caller.form.getPartsArray() />
                <cfif IsDefined("tmpPartsArray")>
                    <cfloop array="#tmpPartsArray#" index="tmpPart">
                        <cfif tmpPart.isFile() and tmpPart.getName() is attributes.filefield>
                            <cfset thisFileName = tmpPart.getFileName() />
                            <cfset thisExt      = listLast(thisFileName, ".")>
                        </cfif>
                    </cfloop>
                </cfif>