ColdFusion: get the name of a file before uploading

user19302 picture user19302 · May 14, 2009 · Viewed 14.3k times · Source

How can I get the filename of a file before I call the

<cffile action = "upload">

? I can get the filename of the temp file, but not of the actual filename. In PHP land I can use the $_FILES superglobal to get what I want - but as far as I can tell no such thing exists in ColdFusion.

I can get the filename client-side but would really want to do this server side.

Thanks

Answer

Ryan Stille picture Ryan Stille · Dec 4, 2012

Yes this is possible. You can use this function to grab the client file name before using the cffile tag:

<cffunction name="getClientFileName" returntype="string" output="false" hint="">
    <cfargument name="fieldName" required="true" type="string" hint="Name of the Form field" />

    <cfset var tmpPartsArray = Form.getPartsArray() />

    <cfif IsDefined("tmpPartsArray")>
        <cfloop array="#tmpPartsArray#" index="local.tmpPart">
            <cfif local.tmpPart.isFile() AND local.tmpPart.getName() EQ arguments.fieldName> <!---   --->
                <cfreturn local.tmpPart.getFileName() />
            </cfif>
        </cfloop>
    </cfif>

    <cfreturn "" />
</cffunction>

More info here: http://www.stillnetstudios.com/get-filename-before-calling-cffile/