ColdFusion:在上传之前获取文件的名称

12 coldfusion file-upload

在调用之前,如何获取文件的文件名

<cffile action = "upload">
Run Code Online (Sandbox Code Playgroud)

?我可以获取临时文件的文件名,但不能获取实际文件名.在PHP领域,我可以使用$ _FILES超全局来获得我想要的东西 - 但据我所知,ColdFusion中不存在这样的东西.

我可以获得文件名客户端,但真的想要这个服务器端.

谢谢

Rya*_*lle 16

是的,这是可能的.在使用cffile标记之前,您可以使用此函数获取客户端文件名:

<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>
Run Code Online (Sandbox Code Playgroud)

更多信息:http://www.stillnetstudios.com/get-filename-before-calling-cffile/


小智 8

我正在使用Railo并找到原始文件名:

GetPageContext().formScope().getUploadResource('your_file_input_form_name').getName();

也许这也适用于adobe服务器?如果你想以某种方式重命名你上传的文件并且不希望它被移动通过两个临时目录(请参阅重命名文件,因为它们被上载(CFFILE实际上如何工作)),这非常方便


Ben*_*oom 7

在调用cffile之前我不知道找出方法,但可能有一个解决方法.

通话时,<cffile action="upload">您可以使用指定结果result="variable".因此,使用目标作为临时文件调用上载.结果变量是一个包含成员clientFile的结构,它是客户端计算机上文件的名称.

现在,您可以使用<cffile action="move">原始文件名执行任何操作.