Jen
Jen

Reputation: 17

CFFILE upload - insert file location into SQL db at the same time?

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>

Upvotes: 0

Views: 518

Answers (2)

Mark A Kruger
Mark A Kruger

Reputation: 7193

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.

Upvotes: 1

Scott Stroz
Scott Stroz

Reputation: 7519

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

Upvotes: 2

Related Questions