ColdFusion: get the name of a file before uploadin

2019-04-21 02:14发布

问题:

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

回答1:

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/



回答2:

I don't know of a way to find out before calling cffile, but there may be a workaround.

When you call <cffile action="upload"> you can specify a result using result="variable". So, call the upload with the destination as a temp file. Your result variable is a struct which contains the member clientFile, which is the name of the file on the client's computer.

Now, you can use <cffile action="move"> to do whatever it is you need to do with the original filename.



回答3:

I'm using Railo and found the original filenames with:

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

maybe this works on an adobe server as well? its quite handy if you want to rename your uploaded file somehow and don't want it to get moved through two temp dirs (see Renaming Files As They Are Uploaded (how CFFILE actually works))



回答4:

WOW, i found a great and easy solution! with a little javascript

In this way you get the temp filename for the cffile upload and the actual file.jpg name for the database

<html>
<head>
<script type="text/javascript">
function PassFileName()
{
document.getElementById("fileName").value=document.getElementById("fileUp").value;
}
</script>
</head>
<body>
<form name="form1" method="post" enctype="multipart/form-data" >
File: <input type="file" name="fileUp" id="fileUp" size="20" onchange="PassFileName()" /> <br />
Title: <input type="text" name="Title" id="Title"><br />
<input type="hidden" id="fileName" size="20" name="fileName" />
<input type="submit" name="submit">
</form>
</body>
</html>


回答5:

Here's how we do it. Basically, there is a file field, and a string field. JavaScript grabs the filename from the browser before the form is submitted. Obviously, you need to verify that the filename on the other end is actually present (it'll be blank if the user has JavaScript disabled, for example) and you'll need to parse the string to handle platform differences (/users/bob/file.jpg versus C:\Documents and Settings\bob\file.jpg)

<script>
    function WriteClientFileName(){
        $('ClientFileName').value = $('ClientFile').value;
    }
</script>

<form enctype="multipart/form-data" onsubmit="WriteClientFileName();">

    <input type="File" name="ClientFile" id="ClientFile">
    <input type="hidden" name="ClientFileName" id="ClientFileName" value="">

    <input type="submit">
</form>

Incidentally, this technique is cross-language. It'll work equally well in RoR, PHP, JSP, etc.

Edit: If a user is "wielding a fierce FireBug" what's the issue? Even if they don't have Firebug, they can still rename the file on their end and change the input. Plus, you're validating your inputs, right?



回答6:

Another option might be to have client-side code populate a hidden form field with the filename, which you would then have server-side.

Ben Doom's answer is generally how I would approach it, though.



回答7:

If you have the name attribute defined on the input control, the file name will be in the FORM scope. For example:

<cfif not structIsEmpty(form)>
    <cfdump var="#form#">
<cfelse>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form method="POST" action="#cgi.SCRIPT_NAME#">
            <input type="file" name="fileIn" />
            <input type="Submit" name="formSubmit">
        </form>
    </body>
    </html>
</cfif>


回答8:

There is no way to know the file name for uploaded files before saving to the server in ColdFuson, Railo or OpenBD. I typically generate 'my' new filename using the createUUID() function in advance of saving the file.