title: Storing file name when uploading using Coldfusion
body: <p>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</p>
  
  <pre><code>&lt;form id="uploadattachment" enctype="multipart/form-data" 
         method="post" action="/governance/attachmentfilestore"&gt;
  
    &lt;cfif isDefined("fileUpload")&gt;
          &lt;cffile action="upload"
                  fileField="fileUpload"
                  accept="application/pdf"
                  nameconflict="makeunique"
                  destination="#ExpandPath( '/files/governance/upr/' )#"&gt;
  
  
         &lt;input type="hidden" name="filename" id="filename" value=""&gt;
         &lt;input type="hidden" readonly id="uprUUID" name="uprUUID" 
                 style="width: 400px" value="&lt;cfoutput&gt;#params.key#&lt;/cfoutput&gt;"/&gt;
         &lt;input type="hidden" readonly id="status" name="status" 
                 style="width: 400px" value="1"/&gt;
         &lt;input name="fileUpload" type="file" style="width: 200px;" /&gt;
         &lt;button type="submit" name="action" 
                 class="submitBtn primary rightSubmitBtnSpace"&gt;Upload&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>Does anyone have a solution on how you can populate a field with the name of the file that is selected to be uploaded?</p>
  
  <p>I have added the CFFILE.serverFile in and it worked once, but I'm guessing thats because it grabbed the previously uploaded files name. </p>
  
  <p>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.</p>
  
  <p>My code looks like this now to try and work around it how ever this doesn't seem to work either. </p>
  
  <pre><code>&lt;cfif isDefined("CFFILE.serverFile")&gt;
      &lt;cfset form.filename = CFFILE.serverFile&gt;
  &lt;cfelse&gt;
       &lt;cfset form.filename = "null"&gt;
  &lt;/cfif&gt;
  &lt;input type="hidden" name="filename" id="filename" 
          value="&lt;cfoutput&gt;#CFFILE.serverFile#&lt;/cfoutput&gt;"/&gt;
  </code></pre>