0

I have a little page that has a form with a few fields in it. Also included is a file upload function. I need to be able to have the filename of the file being uploaded to be inserted into the 'sURL' field, when the form is submitted. (The sURL field can be populated automatically with this filename or can also be manually entered if it is an external URL). I have looked at other people with this issue and there seems to be no straightforward fix? Is anyone able to shed some light?

<html><head>    
<title>New Survey Entry</title>
</head>
<body>

<script language="JavaScript" type="text/javascript">
function check ( form )
{
  if (form.ul_path.value == "") {
    alert( "Please select the file to upload." );
    form.ul_path.focus();
    return false ;
  }
  return true ;
}

</script>

<CFIF NOT isDefined("dir")>
    <CFSET dir = "">
</CFIF>

<CFIF NOT isDefined("clientFile")>
    <CFSET clientFile = "">
</CFIF>

<CFQUERY NAME="getpub" DATASOURCE="testpage" DBTYPE="ODBC">
    SELECT *
    FROM surveypubs
    ORDER BY sGroup asc
</CFQUERY>
<h2><center>NEW SURVEY ENTRY</center></h2>
<table cellpadding="3" cellspacing="3" class="font13">
<cfoutput>
    <form name="input" enctype="multipart/form-data" action="index.cfml?cat=test&page=insertSurvey" method="post">
    <tr>
        <td valign="middle" align="left"><b>Year:</b></td>
        <td colspan="3"><input name="sYear" type="text" size="8" value="<CFOUTPUT>#year(now())#</CFOUTPUT>"><input name="sYear_required" type="hidden" value="You must enter a Year."></td>
    </tr>
    </cfoutput>
    <tr>
        <td valign="middle" align="left"><b>Group:</b></td>
        <td colspan="3"> <select name="sGroup">
<option value="">--- Select One ---</option>
<cfoutput query="getpub"><option value="#sGroup#">#sGroup#</option></cfoutput>
</select> <br> Don't see the Survey Group?  Click <a href="/index.cfml?cat=test&page=inputgroup" target="_blank">here</a> to add.

      </td>

</div><br><br>
    </tr>

    <tr>
        <td valign="middle" align="left"><b>Title:</b></td>
        <td colspan="3"><input name="sTitle" type="text" size="85"><input name="sTitle_required" type="hidden" value="You must enter a Title."></td>
    </tr>

    <tr>
        <td valign="middle" align="left"><b>Comment:</b></td>
        <td colspan="3"><input name="sComment" type="text" size="85"></td>
    </tr>

    <tr>
        <td valign="middle" align="left"><b>URL:</b></td>
        <td colspan="3"><input name="sURL" type="text" size="85"></td>
    </tr>

        <td valign="bottom" align="left"><b>URL Type:</b></td>
        <td colspan="3">
        <input type="radio" name="surlType" value="0" checked> Internal &nbsp;&nbsp;&nbsp;<input type="radio" name="surlType" value="1"> External</td>
    </tr>

<cfoutput>  
<input name="dateAdded" type="hidden" value="#dateformat(now(),"mm-dd-yyyy")#">
</cfoutput>                 
    <tr>
      <td></td>
      <form action="/index.cfml?cat=test&page=inputSurvey" method="POST" enctype="multipart/form-data" name="upload_form" id="upload_form" onsubmit="return check(this);">

    <CFIF structKeyExists(form, "ul_path") and len(form["ul_path"])>

        <CFFILE ACTION="UPLOAD" FILEFIELD="ul_path" DESTINATION="D:\testpage\docs\" NAMECONFLICT="OverWrite">
        <CFSET ClientFilePath = "#clientDirectory#\#clientFile#">
</CFIF>

      <td colspan="3" align="left">Click on the Browse button to select the file to Upload:<br>
      <input type="file" name="ul_path" id="ul_path" style="height: 22px;width: 350px;" value=""></td>
  </tr>
    <tr>
        <td></td>
        <td colspan="3" align="center"><input type="submit" name="submit" value="Submit">&nbsp;&nbsp;<input name="clear" value="Clear" type="reset">&nbsp;&nbsp;<input type="button" name="back" value="Back" class="input" onClick="javascript:history.back();"></td>
    </tr>
    </table>

    </form>

<cfif isDefined("CFFILE.ClientFile")>
    <cfset form.sURL = "#CFFILE.ClientFile#">
<cfelse>
     <cfset form.sURL = "null">
 </cfif>

<cfoutput><input type="hidden" name="sURL" id="sURL" value="http://testpage.com/docs/#ClientFile#"/></cfoutput> 
 </form>      
</body>
    </html>
Jen
  • 17
  • 6
  • 2
    I would suggest not uploading to a web accessible directory. You should be uloading to a temp dir then moving the file to a final destination afterwards. On another note.. why is your upload processor in your form? – Dave Ferguson Sep 05 '13 at 13:28
  • Even better...upload the file to a directory out of the web root, then when you need to serve it, use `cfcontent` – Scott Stroz Sep 05 '13 at 13:30
  • I think this is the best example of what I am trying to do http://stackoverflow.com/questions/14143076/storing-file-name-when-uploading-using-coldfusion The upload processor is in my form because I've been told that is how it has to be. I can't stand it.. but that is another matter. Basically I need it to function so that if a file is uploaded, the filename is automatically added to my sURL field. If there was no file to be uploaded, then the sURL would point to an external link that is manually entered. I hope that makes it a bit clearer. It has been driving me crazy. – Jen Sep 05 '13 at 17:33
  • I am not sure who told you that the cffile tag had to be there but they are wrong. – Dave Ferguson Sep 05 '13 at 18:06

2 Answers2

2

Use the result attribute of cffile, this will then give you a structure of data about your upload, including the directory the file was stored in as well as the file name. Add result='moo' to your cffile call and then do a cfdump of moo to see all the data.

Check out this link for more info on what is returned in result

Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
  • Also, you are interested in the file's location on the *server*. So you should be using the `serverXXX` variables, not `clientXXX`. – Leigh Sep 05 '13 at 13:36
1

I sense that what you are trying to do is not being answered here. What you seem to be trying to do is on the client before the file is uploaded. You wish to collect the name of the file the user has chosen from his or her file system and using JS populate some other form field - which may contain the name of the file or possible some URL value or something else provided by the user.

That's different from collecting the name of the form after it is submitted because it runs afoul of browser protections. I really don't think there's an adequate solution in JS to this. You might find a solution that works in some browsers but I doubt it will be consistent.

I could be wrong of course. I'd be glad to have someone show me a solution.

Mark A Kruger
  • 7,183
  • 20
  • 21