Link to home
Start Free TrialLog in
Avatar of rrattie
rrattieFlag for United States of America

asked on

Making my form submit to itself and upload a file properly (cffile)

I have a detail/update page that has to do with showing the details of a file on a website, and on this page you can also update the details of the file. One update you can do is also upload a more recent copy of the file and overwrite the original.

My problem is I cannot get the file upload to function properly and store the actual file name in the database with the form submitting to the same page.
<cfinclude template="../../../includes/common/KT_common.cfm">
<cfinclude template="../../../includes/tng/tNG.inc.cfm">
<cfscript>
// Make a transaction dispatcher instance
tNGs = Request.tNG_CreateObject("tNG_dispatcher");
tNGs.init("../../../");
</cfscript>
<cfscript>
// Start trigger
formValidation = Request.tNG_CreateObject("triggers.tNG_FormValidation");
formValidation.init();
tNGs.prepareValidation(formValidation);
// End trigger
</cfscript>
<cfscript>
// Make an update transaction instance
upd_Links = Request.tNG_CreateObject("tNG_update");
upd_Links.init("USAMRAA_external");
tNGs.addTransaction(upd_Links);
// Register triggers
upd_Links.registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
upd_Links.registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, formValidation);
upd_Links.registerTrigger("END", "Trigger_Default_Redirect", 99, "index.cfm?page=12&linkID={linkID}");
// Add columns
upd_Links.setTable("Links");
upd_Links.addColumn("linkLabel", "STRING_TYPE", "POST", "linkLabel");
upd_Links.addColumn("URL", "STRING_TYPE", "POST", "URL");
upd_Links.addColumn("linkOrder", "NUMERIC_TYPE", "POST", "linkOrder");
upd_Links.addColumn("pageID", "STRING_TYPE", "POST", "pageID");
upd_Links.addColumn("pubStatus", "NUMERIC_TYPE", "POST", "pubStatus");
upd_Links.addColumn("linkSubCatID", "NUMERIC_TYPE", "POST", "linkSubCatID");
upd_Links.addColumn("linkSubSubCatID", "NUMERIC_TYPE", "POST", "linkSubSubCatID");
upd_Links.addColumn("linkLabelAcronym", "STRING_TYPE", "POST", "linkLabelAcronym");
upd_Links.addColumn("pageAnchors", "STRING_TYPE", "POST", "pageAnchors");
upd_Links.addColumn("lastUpdated", "DATE_TYPE", "POST", "lastUpdated");
upd_Links.addColumn("filename", "STRING_TYPE", "POST", "uploadfile");
upd_Links.addColumn("fileType", "STRING_TYPE", "POST", "filetype");
upd_Links.setPrimaryKey("linkID", "NUMERIC_TYPE", "GET", "linkID");
</cfscript>
<cfscript>
// Execute all the registered transactions
tNGs.executeTransactions();
</cfscript>
<cfscript>
// Get the transaction recordset
rsLinks = tNGs.getRecordset("Links");
</cfscript>
<cfquery name="qGetPageName" datasource="external">
Select *
From pages
Order By PageName 
</cfquery>
<cfquery name="qGetLinkType" datasource="external">
Select *
From LinkType
Order By linkType 
</cfquery>
<cfquery name="getSubCategory" datasource="external" >
SELECT *
FROM   LinksubCat
Order by subCatName 
</cfquery>
<cfquery name="getSubSubCategory" datasource="external">
SELECT *
FROM LinksubSubCat
Order by linksubSubCatName 
</cfquery>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Detail/ Update</title>
<script src="../../../includes/common/js/base.js" type="text/javascript"></script>
<script src="../../../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../../../includes/skins/style.js" type="text/javascript"></script>
<link href="../../../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<cfoutput>#tNGs.displayValidationRules()#</cfoutput>
</head>
<body>
<cfparam name="FORM.uploadfile" default="">
<cfparam name="FORM.filetype" default="">
<cfoutput>#tNGs.getErrorMsg()#</cfoutput>
<cfform action="<cfoutput>#Request.KT_escapeAttribute(Request.KT_getFullUri())#</cfoutput>" method="post" enctype="multipart/form-data" id="form1">
  <h3>Editing: <cfoutput>#rsLinks.linkLabel#</cfoutput></h3>
  <table cellpadding="2" cellspacing="0" class="KT_tngtable" id="standard_table" >
    <tr>
      <td class="KT_th"><label for="linkLabel">Link Label:</label></td>
      <td><input type="text" name="linkLabel" id="linkLabel" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.linkLabel)#</cfoutput>" size="100" />
        <cfoutput>#tNGs.displayFieldHint("linkLabel")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "linkLabel")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="URL">File Path:</label></td>
      <td><input type="text" name="URL" id="URL" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.URL)##Request.KT_escapeAttribute(rsLinks.fileName)#</cfoutput>" size="70" />
        <cfoutput>#tNGs.displayFieldHint("URL")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "URL")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="uploadfile">Update File:</label></td>
      <td><input name="uploadfile1" type="file" id="uploadfile1" size="70" />
        <label for="filetype">File Type:</label>
        <select name="filetype" id="filetype">
          <option selected="selected">Select One</option>
          <option value="doc">DOC</option>
          <option value="pdf">PDF</option>
          <option value="ppt">PPT</option>
        </select></td>
    </tr>
    <tr>
      <td class="KT_th"><label for="linkOrder">Order:</label></td>
      <td><input type="text" name="linkOrder" id="linkOrder" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.linkOrder)#</cfoutput>" size="32" />
        <cfoutput>#tNGs.displayFieldHint("linkOrder")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "linkOrder")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="pageID">Page ID:</label></td>
      <td><select name="pageID" id="pageID">
          <option value="" <cfif (isDefined("rsLinks.pageID") AND #qGetPageName.PageID# EQ "#rsLinks.pageID#")>selected</cfif>><cfoutput>#qGetPageName.PageName#</cfoutput></option>
          <cfoutput query="qGetPageName">
            <option value="#qGetPageName.PageID#" <cfif (isDefined("rsLinks.pageID") AND #qGetPageName.PageID# EQ "#rsLinks.pageID#")>selected</cfif>>#qGetPageName.PageName# - #qGetPageName.PageID#</option>
          </cfoutput>
        </select>
        <cfoutput>#tNGs.displayFieldHint("pageID")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "pageID")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="pubStatus">Published?</label></td>
      <td><select name="pubStatus" id="pubStatus">
          <cfoutput>
            <cfif #rsLinks.pubStatus# EQ "1">
              <option value="1" selected>Yes</option>
              <option value="0" >No</option>
              <cfelse>
              <option value="0" selected>No</option>
              <option value="1" >Yes</option>
            </cfif>
            </option>
          </cfoutput>
        </select>
        <cfoutput>#tNGs.displayFieldHint("pubStatus")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "pubStatus")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="linkSubCatID">SubCategory:</label></td>
      <td><select name="linkSubCatID" id="linkSubCatID">
          <option value="" <cfif (isDefined("rsLinks.linkSubCatID") AND #getSubCategory.subCatID# EQ "#rsLinks.linkSubCatID#")>selected</cfif>><cfoutput>#getSubCategory.subCatName#</cfoutput></option>
          <cfoutput query="getSubCategory">
            <option value="#getSubCategory.subCatID#" <cfif (isDefined("rsLinks.linkSubCatID") AND #getSubCategory.subCatID# EQ "#rsLinks.linkSubCatID#")>selected</cfif>>#getSubCategory.subCatName#</option>
          </cfoutput>
        </select>
        <cfoutput>#tNGs.displayFieldHint("linkSubCatID")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "linkSubCatID")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="linkSubSubCatID">Sub-SubCategory:</label></td>
      <td><select name="linkSubSubCatID" id="linkSubSubCatID">
          <option value="" <cfif (isDefined("rsLinks.linkSubSubCatID") AND #getSubSubCategory.subCatID# EQ "#rsLinks.linkSubSubCatID#")>selected</cfif>><cfoutput>#getSubSubCategory.linksubSubCatName#</cfoutput></option>
          <cfoutput query="getSubSubCategory">
            <option value="#getSubSubCategory.subSubCatID#" <cfif (isDefined("rsLinks.linkSubSubCatID") AND #getSubSubCategory.subSubCatID# EQ "#rsLinks.linkSubSubCatID#")>selected</cfif>>#getSubSubCategory.linksubSubCatName#</option>
          </cfoutput>
        </select>
        <cfoutput>#tNGs.displayFieldHint("linkSubSubCatID")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "linkSubSubCatID")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="linkLabelAcronym">Acronym:</label></td>
      <td><input type="text" name="linkLabelAcronym" id="linkLabelAcronym" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.linkLabelAcronym)#</cfoutput>" size="32" />
        <cfoutput>#tNGs.displayFieldHint("linkLabelAcronym")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "linkLabelAcronym")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="pageAnchors">Page Anchors:</label></td>
      <td><input type="text" name="pageAnchors" id="pageAnchors" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.pageAnchors)#</cfoutput>" size="32" />
        <cfoutput>#tNGs.displayFieldHint("pageAnchors")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "pageAnchors")#</cfoutput> </td>
    </tr>
    <tr>
      <td class="KT_th"><label for="lastUpdated">Last Updated:</label></td>
      <td><input type="text" name="lastUpdated" id="lastUpdated" value="<cfoutput>#Request.KT_escapeAttribute(rsLinks.lastUpdated)#</cfoutput>" size="32" />
        <cfoutput>#tNGs.displayFieldHint("lastUpdated")#</cfoutput> <cfoutput>#tNGs.displayFieldError("Links", "lastUpdated")#</cfoutput> </td>
    </tr>
    <tr class="KT_buttons">
      <td colspan="2"><input type="submit" name="KT_Update1" id="KT_Update1" value="Update record" />
      </td>
    </tr>
  </table>
</cfform>
<cfif #FORM.filetype# EQ "pdf">
  <cfset uploadDestination = "C:\Inetpub\wwwroot\internet\site\pages\pdf\">
  <cfelseif #FORM.filetype# EQ "ppt">
  <cfset uploadDestination = "C:\Inetpub\wwwroot\internet\site\pages\ppt\">
  <cfelseif #FORM.filetype# EQ "doc">
  <cfset uploadDestination = "C:\Inetpub\wwwroot\internet\site\pages\doc\">
  <cfelseif #FORM.filetype# EQ "">
  <cfset uploadDestination = "C:\Inetpub\wwwroot\internet\site\pages\">
</cfif>
<cfset files = ArrayNew(1)>
<cfloop from="1" to="1" index="i" >
  <cffile action="upload" destination="#uploadDestination#" filefield="FORM.uploadfile#i#" nameconflict="makeunique">
  <!--- add the file info to our array --->
  <!--- append the file data to the array --->
  <cfset ArrayAppend(files, Duplicate(cffile))>
  <!--- append some value representing "no file"--->
  <cfset ArrayAppend(files, "")>
</cfloop>
<cfif IsStruct( files[1] ) AND structKeyExists( files[1], "ServerFile")>
  <cfparam name="uploadfile"  default="#files[1].ServerFile#">
</cfif>
<cfdump var="#form#">
<p>&nbsp;</p>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of 3ComGSO
3ComGSO

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zvonko
You do handle the file upload at the bottom of the page.
Place the upload at the top of the page processing.
Of course you have to move up also all relevant tags like cfparam and so on.