3

I am trying to store the filename of the selected file to be uploaded into a hidden input field on the form. my form looks like this

<form id="uploadattachment" enctype="multipart/form-data" 
       method="post" action="/governance/attachmentfilestore">

  <cfif isDefined("fileUpload")>
        <cffile action="upload"
                fileField="fileUpload"
                accept="application/pdf"
                nameconflict="makeunique"
                destination="#ExpandPath( '/files/governance/upr/' )#">


       <input type="hidden" name="filename" id="filename" value="">
       <input type="hidden" readonly id="uprUUID" name="uprUUID" 
               style="width: 400px" value="<cfoutput>#params.key#</cfoutput>"/>
       <input type="hidden" readonly id="status" name="status" 
               style="width: 400px" value="1"/>
       <input name="fileUpload" type="file" style="width: 200px;" />
       <button type="submit" name="action" 
               class="submitBtn primary rightSubmitBtnSpace">Upload</button>
</form>

This is then sent to the controller which writes it to the database how ever I cannot work out a way to get the name of the file to store in the "filename" field.

Does anyone have a solution on how you can populate a field with the name of the file that is selected to be uploaded?

I have added the CFFILE.serverFile in and it worked once, but I'm guessing thats because it grabbed the previously uploaded files name.

Now when loading the page I get Serverfile is undefined in CFFILE and so it does not let me populate the form with the files name.

My code looks like this now to try and work around it how ever this doesn't seem to work either.

<cfif isDefined("CFFILE.serverFile")>
    <cfset form.filename = CFFILE.serverFile>
<cfelse>
     <cfset form.filename = "null">
</cfif>
<input type="hidden" name="filename" id="filename" 
        value="<cfoutput>#CFFILE.serverFile#</cfoutput>"/>
Leigh
  • 28,765
  • 10
  • 55
  • 103
ChayC
  • 45
  • 1
  • 6

3 Answers3

6

The filename does not become available until the file is uploaded. This happens after the form is posted. The only way around this is to try posting the fileupload via AJAX and then returning the filename.

Otherwise, you can assign the value to the field after the file is upload and the form is posted.

    <cfset form.filename = CFFILE.serverfile>
Leigh
  • 28,765
  • 10
  • 55
  • 103
Sollinger04
  • 385
  • 1
  • 9
  • 1
    Getting the name after the updload is my preference. It's simpler, and given the attribute nameconflict="makeunique", it guarantees a correct answer. – Dan Bracuk Jan 03 '13 at 17:13
  • fyi, removed [deprecated `file` scope](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fa1.html) – Leigh Jan 03 '13 at 17:25
  • 1
    It should be `` Also, be sure to check the docs for cffile. there are a ton of values available in the cffile structure http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_10.html – genericHCU Jan 03 '13 at 18:50
  • 2
    @Webgod, If you click the link Leigh provided and follow the `action="upload"` link or the link I gave, you will see `Note: The file prefix is deprecated, in favor of the cffile prefix. Do not use the file prefix in new applications.` I saw she tried to help you with that in another post. `CFFile.serverFile` good. `File.serverFile` bad :) – genericHCU Jan 03 '13 at 18:58
  • Thanks guys. I knew that, but have been living in the CF8 world for so long, that I just typed out of habit. Thanks for steering me right. – Sollinger04 Jan 03 '13 at 20:18
0

You can find the file name before saving.

Railo:

GetPageContext().formScope().getUploadResource("myFormField").getName()

Adobe:

function getClientFileName(fieldName) {
var tmpPartsArray = Form.getPartsArray();
var clientFileName = "";

if (IsDefined("tmpPartsArray")) {
    for (local.tmpPart in tmpPartsArray) {
        if (local.tmpPart.isFile() AND local.tmpPart.getName() EQ arguments.fieldName) {
            return local.tmpPart.getFileName();
            }
        }
    }

return "";
}

Source: http://www.stillnetstudios.com/get-filename-before-calling-cffile/

Scott Coldwell
  • 868
  • 7
  • 14
0

As lvmisooners said,

GetPageContext().formScope().getUploadResource("myFormField").getName()

works for Railo (and Lucee) but I noticed an interesting wrinkle: if the browser is IE than this returns the full source path including the filename. Firefox and Chrome on the other hand, return only the filename.

For my application I need the full path, but haven't been able to find that if the browser is FireFox or Chrome. If anyone has any ideas I would be most grateful!

(Sorry for not replying to lvmisooners but I don't have the reputation points to reply.)

Mike
  • 93
  • 7