Solved

Form error: "The form field DocumentImage did not contain a file."

Posted on 2010-09-09
44
383 Views
Last Modified: 2012-05-10
CF 8
SQL Server 2005

Task in brief: My ColdFusion form gives me an error: "The form field DocumentImage did not contain a file."

Task details:

Many experts have helped me with this task and are familiar with it; this is the CEP document management task. gdemaria, _agx_, azadi, Plucka, myselfrandhawa, Brichsoft have helped me very patiently with different aspects of this task since ... last March. The project has come a long way since then.

In my CEP document management application I created a way to upload a small GIF, JPEG, or PNG file (a document "thumbnail" image) and associate it with a documentID. That works, as long as I browse for a file and upload the file every time I process my form. The form uploads an image to a server folder that is not in web root, and updates column DocumentImage in table tbl_CEP_Documents.

But: if I submit the form, but do not browse for a file, I get this error: "The form field DocumentImage did not contain a file."

So, my question: How can I get the form to accept an empty value for the input: <cfinput type="file" name="DocumentImage">

<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

Open in new window


I am sure I am missing something very simple.

I am also curious to see if my delete DocumentImage checkbox and query will work; but I can begin to test that once I solve this form error problem.

I include my code below, with hints where to find the code that is relevant to this question. Thank you as always for any advice.

Eric B

1. form "file" input to select image file with name "DocumentImage" to upload: line 612



<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />



2. form checkbox input to mark image for deletion: line 642 (checkbox has name: deleteDocumentImage)



<input type="checkbox" name="deleteDocumentImage" value="#getDocumentImage.DocumentImage#" class="border0" />



3. CFFILE tag for DocumentImage column: line 95



<cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">



4. delete DocumentImage query: line 112



 <cfquery name="qry_deleteDocumentImage" datasource="#ds#">

       			DELETE FROM tbl_CEP_Documents

           		WHERE DocumentImage = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

      		</cfquery>







<!---

Name:        insert_update.cfm

Author:      Eric B, following Ben Forta code in ColdFusion 8 web application construction kit; gdemaria, _agx_, azadi, Plucka, myselfrandhawa, Brichsoft

Description: CEP add and update documents administration

Created:     2/24/2010 - 8/23/2010

ColdFusion Version 8

MS SQL Server 2005

--->





 <!--- Set default value for FileID in scope FORM --->

<cfparam name="form.FileID" default="">



 <!--- Set default value for NumberFiles in scope FORM --->

<cfparam name="form.NumberFiles" default="">



 <!--- Set default value for SelectDocumentTopics in scope FORM --->

<cfparam name="form.SelectDocumentTopics" default="0">





 <!--- Set default value for DocumentID in scope URL --->

<cfparam name="URL.DocumentID" default="">



 <!--- Define DocumentID in scope FORM, then set form.DocumentID equal to the DocumentID passed in the URL: for use later in the application --->

<cfparam name="form.DocumentID" default="#URL.DocumentID#">







<!--- default isPressRelease is 0 (false) --->

<cfparam name="form.isPressRelease" default="0">









 <!--- Set datasource --->

 <cfset ds="ebwebwork">

  

 <!---- begin CFTRY; catch any errors, whether you throw them or the database does; and to test that file were uploaded successfully, or not  ---->

 <cftry>  

 



<!---- populate this with an error message ---->

<cfset variables.error = ""> 





        <!--- BEGIN: Save action --->





<!--- begin form.doSave --->



<!--- when the user clicks the Save Button, these events can occur: Add a document; Update a document; Delete a Document; Delete a file that is associated with a document --->



<cfif IsDefined("FORM.doSave")>





<!--- make sure that documentTitle and documentType are entered --->  

    

	<cfif len(form.DocumentTitle) eq 0>

			  <cfthrow message="Document Title is required">

	</cfif>

    

    <cfif len(form.DocumentType) eq 0>

			  <cfthrow message="Document Type is required">

	</cfif>





 <!--- in this query select NOTHING from table tbl_CEP_documents, and simply check if DocumentTitle exists --->

 

 <cfquery datasource="#ds#" name="CheckDocumentTitle">

  SELECT 'Nothing' FROM tbl_CEP_Documents

  WHERE DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.DocumentTitle#">

  AND DocumentID <> <cfqueryparam cfsqltype="cf_sql_integer" value="#val(Form.DocumentID)#">

  </cfquery>

   

 

  

   <!--- if DocumentTitle exists, display error; refuse record insert --->

   

	<cfif CheckDocumentTitle.recordcount GT 0>

		   <cfthrow message="The Document Title is already taken; please enter another title">

	</cfif>

    

    <!--- begin statements to update, insert database records --->  

   

    <!--- begin cfif val(form.DocumentID) --->

    

    <!--- the action is UPDATE; a DocumentID Exists --->

    

				<cfif val(form.DocumentID)>

                      

             <!--- group three queries in a cftransaction: UPDATE Document; DELETE Document Topics; INSERT (new) Document Topics--->

<cftransaction>

         

         

         <!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

                      <cfif form.DocumentImage is not "">

         

	<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

    	<!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

         

         

           <!--- DELETE DocumentImage --->

    

    	   <!--- begin CFELSEIF isdefined(form.deleteDocumentImage) --->

		 <cfelseif isDefined("form.deleteDocumentImage")>        

   

	<!---- query to delete DocumentImage for selected DocumentID ---->

    	   <cfquery name="qry_deleteDocumentImage" datasource="#ds#">

       			DELETE FROM tbl_CEP_Documents

           		WHERE DocumentImage = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

      		</cfquery>

         

                </cfif>

<!----- END form.DocumentImage is not "" ---->



    

  			  <cfquery name="UpdateDocument" datasource="#ds#">

				  UPDATE tbl_CEP_Documents

				  SET   DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

					DocumentType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

					DocumentAuthor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

				    DocumentAbstract = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  				    DocumentKeyword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                    DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">,

				    DocumentPublicationDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

				    DateRecordModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				  WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			</cfquery>





<cfquery name="UpdateDocumentDeleteTopics" datasource="#ds#">

   DELETE FROM tbl_Document_Has_Topic

   WHERE DocumentID = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

</cfquery>



<cfquery name="UpdateDocumentInsertNewTopics" datasource="#ds#">

INSERT INTO tbl_Document_Has_Topic

		(

        DocumentID

        ,  DocumentTopicID

        )

SELECT   <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

         , DocumentTopicID

FROM     tbl_CEP_Document_Topic

WHERE   DocumentTopicID  IN

(

<cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

)

</cfquery>

</cftransaction>







    <!--- CFELSE: DocumentID does not exist, then insert new document --->

				<cfelse> 

                

                

      <!--- use two queries: one to insert the record into tbl_CEP_Documents; one to insert DocumentID and DocumentTopicID into tbl_Document_Has_Topic  --->

      <!--- use cftransaction so both queries are processed together  --->



        <cftransaction>

	

				<!---- query to insert new document record into tbl_CEP_Documents ---->

					<cfquery name="InsertDocument" datasource="#ds#" result="newDocument">

				 INSERT INTO tbl_CEP_Documents

     					(

			            DocumentTitle,

            		    DocumentType,

                		DocumentAuthor,

		                DocumentAbstract,

                        DocumentKeyword,

                        DocumentImage,

        		        DocumentPublicationDate,

                		SSMA_TimeStamp

		                )

			     VALUES(

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                          <cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">,

						  <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

		     			  <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				         )         

					</cfquery>

                  

    <!--- cfquery to get the selected document topics, and insert them into tbl_Document_Has_Topic along with the document ID --->

            <cfquery name="InsertDocumentTopics" datasource="#ds#">

                 INSERT INTO tbl_Document_Has_Topic

                 		(

                        DocumentID,

                        DocumentTopicID

                        )

                  SELECT

                   <cfqueryparam value="#newDocument.IDENTITYCOL#" cfsqltype="cf_sql_integer">

                   , DocumentTopicID

                   FROM  tbl_CEP_Document_Topic

                   WHERE   DocumentTopicID  IN

                  (

                     <cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

                  )

               </cfquery>

        </cftransaction>     

             

                  

                    

	 <!--- use the result attribute value to set the form field value --->

      <cfset form.DocumentID = newDocument.IDENTITYCOL>



		<!--- END queries to update or insert database records ---> 



        <!--- END cfif val(form.DocumentID) -- if a document needed to be updated, or added, then it was done --->

					    </cfif>  

    

    

    

	   <!--- ============= BEGIN query to update column FileLinkText in tbl_CEP_Files ============= --->   

              <!--- this section must be placed within the Save Action --->  

 

        <cfloop index="kk" from="1" to="#val(form.numberFiles)#"> 

            <cfset form.updateFileLinkText = form['updateFileLinkText' & kk]>

            <cfset form.fileID = form['fileID' & kk]>

        

             <cfquery name="query_updateFileLinkText" datasource="#ds#">

                UPDATE tbl_CEP_Files

                SET FileLinkText = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.updateFileLinkText)#">

                WHERE FileID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.fileID)#">

             </cfquery>            

         

        </cfloop>

     

        <!--- ============= END query to update column FileLinkText in tbl_CEP_Files ============= --->   

    



    

<!--- remember cfif IsDefined("FORM.doSave") is still defined -- remember to close the cfif later --->

    

  

				<!--- test to see whether or not a file is being uploaded -- does form field FileName contain a file? --->         

  

		    	<!--- BEGIN: if form field FileName contains a file, then upload the file and the file's metadata --- this function updates the tbl_CEP_Files--->

      		<cfif form.fileName is not "">

     

              

			    <!--- BEGIN: upload a file using CFFILE --->       

     			<!--- cffile upload --->       

   			      <cffile action="upload" filefield="FileName" destination="c:\upload\cep-dc.org" nameconflict="overwrite">



			<cfif listFindNoCase("doc,docx,jpg,jpeg,png,gif,pdf,ppt,xls,xlsx,txt", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your file did not upload. You may upload only permitted file types.">

			</cfif>

            



			<!---- query to insert new record into tbl_CEP_Files, with a file  (DOC, JPEG, PDF, etc.) to upload ---->

			<cfquery name="InsertFile" datasource="#ds#">

			 INSERT INTO tbl_CEP_Files

     			(

                DocumentID,

                FileName,

                FileLinkText,

				FileExtension,

				FileType,

				FileSize,

                isDeleted,

                isPressRelease

                )

		     VALUES(

        		  <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">

                  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.FileLinkText)#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFileExt#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.contentType#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.fileSize#">

                  ,0

                  ,0

		           )         

				</cfquery>  

  

		   <!--- END query to insert new record into tbl_CEP_Files --->  

 

 

     

              <!--- DELETE A FILE --->

      <!--- this query must be placed within the Save Action --->

 

    

    	   <!--- begin CFELSEIF isdefined(form.deleteFileID) --->

		 <cfelseif isDefined("form.deleteFileID")>        

   

	<!---- query to set a file to isDeleted = 1 ---->

    	   <cfquery name="qry_deleteFile" datasource="#ds#">

       			UPDATE tbl_CEP_Files

           		SET IsDeleted = 1

        		WHERE FileID IN (<cfqueryparam cfsqltype="cf_sql_integer" value="#form.deleteFileID#" list="yes">)

      		</cfquery>





    <!--- SET OR UNSET A FILE TO isPressRelease --->

      <!--- this query must be placed within the Save Action --->

      

      <!--- Use #form.documentID# to identify all files associated with the current document --->

 

    	   <!--- begin CFELSEIF isDefined(form.isPressRelease) --->

           <!--- use #form.isPressRelease# to exclude checked files --->

		 

		 

		 <cfelseif isDefined("form.isPressRelease")>

   

   <cftransaction>

   <!---- query to handle unchecked files (set isPressRelease = 0---->

  <cfquery name="uncheckedFiles" datasource="#ds#">

            UPDATE      tbl_CEP_files

            SET     isPressRelease = 0

            WHERE      documentID = <cfqueryparam value="#form.documentID#" cfsqltype="cf_sql_integer"> 

            <cfif listLen(form.isPressRelease)>

            AND            fileID NOT IN 

                        (

                              <cfqueryparam value="#form.isPressRelease#" list="true" cfsqltype="cf_sql_integer">

                        )

            </cfif>

      </cfquery>







	<!---- query to handle checked files (set isPressRelease = 1) ---->



      <cfif listLen(form.isPressRelease)>

            <cfquery name="checkedFiles" datasource="#ds#">

            UPDATE      tbl_CEP_files

            SET     isPressRelease = 1

            WHERE      fileID IN 

                        (

                              <cfqueryparam value="#form.isPressRelease#" list="true" cfsqltype="cf_sql_integer">

                        )

            </cfquery>

      </cfif>

      

</cftransaction>



  

    



            </cfif>

<!----- END form.fileName is not "" test to see if file is being uploaded ---->







	<!--- if insert record succeeded, and / or file upload succeeded, and / or FileLinkText were updated, then go to index page --->

				     <cflocation url="index.cfm" addtoken="no">



        

                    

    <!--- END: Save action --->

    



  

        <!--- BEGIN: Delete action --->





			   <!--- delete a database record --->

					   <!--- begin CFELSEIF isdefined(form.doDelete) --->

					        <cfelseif isDefined("form.doDelete")>        

					        <!---- query to delete record ---->

                            

                            <cftransaction>

					       	<cfquery name="DeleteDocument" datasource="#ds#">

					         DELETE FROM tbl_CEP_Documents

					         WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

					        </cfquery>

                                                        

                            <cfquery name="UpdateDocumentDeleteTopics" datasource="#ds#">

							   DELETE FROM tbl_Document_Has_Topic

							   WHERE DocumentID = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

							</cfquery>

                            </cftransaction>

              

 					        <!---- after delete, go to index page ---->

						 <cflocation url="index.cfm" addtoken="no">



	    <!--- END: Delete action --->

        

  



  <!--- END form.doSave --->

</cfif>











       <!--- this CFCATCH will trap errors -- the ones you threw or just regular database issues --->

<cfcatch type="Any">

     <cfset variables.error = cfcatch.message>

</cfcatch>



  

  

  

		  <!--- END CFTRY --->  

			</cftry>







<!--- fetch the data from the database only when there are no errors.

      if an error exists, then let the form variables pass back into the form to display ---->

 

  <cfif len(variables.error) eq 0>

    

			  <!--- get data from table tbl_CEP_Documents and convert the data into form variables --->

			  <cfquery name="getDocumentDetails" datasource="#ds#">

				    select * from tbl_CEP_Documents where DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			  </cfquery>



  			<cfloop index="aCol" list="#getDocumentDetails.columnList#">

			       <cfset "form.#aCol#" = getDocumentDetails[aCol][getDocumentDetails.currentRow]>

			  </cfloop>

    

	</cfif>











<!--- BEGIN HTML / CSS PAGE HEADER --->

<cfinclude template="/cep/cep_header_admin.cfm" />









<!--- if there an error, display error in human readable form --->



<cfif len(variables.error)> 

			 <cfoutput>

			 <div style="border: 1px solid red; padding: 10px; margin:20px; width:400px;">#variables.error#</div>

			 </cfoutput>

</cfif>















				<!----- if record already exists (it will have an ID) then update it; otherwise, add new record... ----->

				<cfif val(form.documentID)>

					  <cfset FormTitle="Edit Document">

					  <cfset ButtonText="Update This Document">

  					  <cfset FileLinkTextButton="Update File Link Text">

				<cfelse>

						<cfset FormTitle="Add a Document">

						<cfset ButtonText="Add Document">



				</cfif>

                

                

  

				<!--- Add or Update Document Form begins here --->

				<cfform method="post" enctype="multipart/form-data" scriptsrc="#Request.CFFORM_JS_LIB#">





 <!--- Embed documentID (PK), fileID, numberFiles as hidden fields, to assign a value to each --->

 <cfoutput>

<input type="hidden" name="documentID" value="#form.documentID#" />



   </cfoutput>







  <cfoutput>



      <h2>#FormTitle#</h2>



  </cfoutput>

  

  

  <!--- BEGIN TABLE ADMIN; DISPLAY DOCUMENT DETAILS --->

 

<table class="table_admin">

<cfif val(form.documentID)>

 <tr>

  <td>

<p><strong>Document ID: <cfoutput>#URL.DocumentID#</cfoutput></strong></p>

  </td>

 </tr>

 <cfelse>

 </cfif>



 <tr>

  <td>

   <p><strong>Document Title</strong><br />



   <cfinput type="Text"

            name="DocumentTitle"

            value="#form.DocumentTitle#"

            message="Document title is required! Please restrict the Document title to 100 characters or fewer."

            required="Yes"

            validateAt="onSubmit,onServer"

            size="100"

            maxlength="250" /></p>

  </td>

 </tr>

 <tr>

  <td>

   <p><strong>Document Type</strong>  <a href="#" class="hintanchor" onMouseover="showhint('Don\'t worry too much about document type. Specify document type \'Report\' for most CEP documents.', this, event, '200px')">[?]</a></p>



   <select name="DocumentType" value="#form.DocumentType#">

   			<option value="Report">Report</option>

            <option value="Policy Brief">Policy Brief</option>

            <option value="Press Release">Press Release</option>

            <option value="Article">Article</option>

            <option value="Summary">Summary</option>

            <option value="Letter">Letter</option>

            <option value="Letter">Pamphlet</option>

            <option value="Letter">Information Bulletin</option>

            <option value="Audio Transcript">Audio Transcript</option>

            <option value="Text Transcript">Text Transcript</option>

   </select>

  </td>

 </tr>





 <tr>

  <td>

   <p><strong>Document Author</strong><br />



   <cfinput type="Text"

            name="DocumentAuthor"

            value="#form.DocumentAuthor#"

            message="Enter Document Author Name"

            required="yes"

            validateAt="onSubmit,onServer"

            size="100"

            maxlength="100" /></p>

  </td>

 </tr>

 

 

<!--- this query requests values from columns DocumentTopicID and DocumentTopic in tbl_CEP_Document_Topics --->

 <cfquery datasource="#ds#" name="GetDocumentTopics">

SELECT DocumentTopicID, DocumentTopic

FROM tbl_CEP_Document_Topic

ORDER BY DocumentTopic

  </cfquery>

 

  <tr>

  <td>

    <p><strong>Assign this Document to CEP Topics</strong> <a href="#" class="hintanchor" onMouseover="showhint('Assign this document to a topic', this, event, '200px')">[?]</a></p>

    

    

      <p class="small">Choose at least one topic. To select more than one topic, hold the Ctrl or the Shift key (on your keyboard) and select topics with the mouse cursor.</p>

   



<!--- query to get existing topics for the current documentID --->



       <cfquery datasource="#ds#" name="GetSelectedTopics">

              SELECT DocumentTopicID

              FROM    tbl_Document_Has_Topic

              WHERE  DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.documentID)#">

       </cfquery>

     

       <!--- convert the DocumentTopicID values to a list --->

      <cfset form.SelectDocumentTopics = ValueList(GetSelectedTopics.DocumentTopicID)>



<cfselect name="SelectDocumentTopics"

size="8"

multiple="yes"

query="GetDocumentTopics"

queryPosition="below"

value="DocumentTopicID"

display="DocumentTopic"

selected="#form.SelectDocumentTopics#">

     <option value="0">Choose topic:</option>

</cfselect>

 

  </td>

 </tr>

 



 <tr>

  <td>

   <p><strong>Document Abstract</strong><br />

   <cfoutput>

   <textarea name="DocumentAbstract" wrap="virtual" style ="width:600px; height:250px;">#form.DocumentAbstract#</textarea>

   </cfoutput></p>

  </td>

 </tr>

 

  <tr>

  <td>

   <p><strong>Document Keywords</strong> <a href="#" class="hintanchor" onMouseover="showhint('Enter keywords as a simple list, without punctuation.', this, event, '200px')">[?]</a><br />



   <cfinput type="Text"

            name="DocumentKeyword"

            value="#form.DocumentKeyword#"

            size="200"

            maxlength="255" /></p>

  </td>

 </tr>

 

 

  <!--- begin DocumentImage table row ---> 



  <tr>

  <td>

   <p><strong>Document Thumbnail Image</strong> <a href="#" class="hintanchor" onMouseover="showhint('Browse local computer for thumbnail image', this, event, '200px')">[?]</a><br />



<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" /></p>



 

    <!--- query to get columns and values from tbl_CEP_files, for use below --->

<cfquery name="getDocumentImage" datasource="#ds#">

    SELECT DocumentImage

    FROM tbl_CEP_Documents

    WHERE DocumentID=<cfqueryparam value="#val(form.DocumentID)#" cfsqltype="cf_sql_integer"> 

</cfquery>





<!--- begin table to display the file called by query getDocumentImage ---> 



<cfif getDocumentImage.recordCount>

   <table class="tablecontrolpanel width600px">

     <tr>

      <td><strong>Thumbnail image file associated with this CEP document:</strong></td>

      <td><strong>Mark for Deletion</strong>  <a href="#" class="hintanchor" onMouseover="showhint('If you want this file to no longer associate with this document, you can mark the file for deletion; the file will be deleted from the server', this, event, '200px')">[?]</a></td>

     </tr>

     

	 <cfoutput>

      

     <tr>





        <!---  get list of file names --->  

       <td>#getDocumentImage.DocumentImage#</td>

       



         <!--- Mark For Deletion checkbox --->      

       <td><input type="checkbox" name="deleteDocumentImage" value="#getDocumentImage.DocumentImage#" class="border0" /></td>



     

    

</tr>



   

     </cfoutput>





   </table>



  <cfelse>

   <p><strong>At the moment, no thumbnail image files are attached to this document.</strong></p>

  </cfif>

<!--- end table to display the file called by query getDocumentImage ---> 







  </td>

 </tr>

 <!--- end DocumentImage table row; the table markup gets confusing; be careful ---> 







 

 <tr>

  <td>

   <p><strong>Publication Date (use form MM/DD/YYYY; please click the calendar icon, below)</strong><br />

   <cfinput name="DocumentPublicationDate"

            value="#DateFormat(DocumentPublicationDate, "mm/dd/yyyy")#"

            message="Publication Date must be a valid date!"

            required="no"

            validate="usdate"

            validateAt="onSubmit,onServer"

            size="10"

            maxlength="10"

            type="datefield" mask="MM/DD/YYYY" /></p>

  </td>

 </tr>

 </table>



  <!--- END TABLE ADMIN; DISPLAY DOCUMENT DETAILS --->

  







   <!--- display the files currently associated with the document --->



<cfif val(form.documentID)>

<p>Below, please see the files currently associated with Document ID <strong><cfoutput>#URL.DocumentID#</cfoutput></strong>. If there are no files associated with the document, then no files will appear below. You have the option to upload a file (PDF, DOC, image file), which will associate the file with this document.</p>

<cfelse>

<p>This is a new document. You have the option to upload a file (PDF, DOC, image file), which will associate the file with this document. Large files will take some time to upload; please be patient.</p>

</cfif>





   <!--- query to get columns and values from tbl_CEP_files, for use below --->

<cfquery name="getFiles" datasource="#ds#">

    SELECT *

    FROM tbl_CEP_files

    WHERE DocumentID=<cfqueryparam value="#val(form.DocumentID)#" cfsqltype="cf_sql_integer"> 

    AND isDeleted = 0

  </cfquery>

  



  <cfif getFiles.recordCount>

   <table class="tablecontrolpanel width600px">

     <tr>

      <td><strong>File ID</strong></td>

      <td><strong>Files associated with this CEP document:</strong></td>

      <td><strong>File Link Text</strong> <a href="#" class="hintanchor" onMouseover="showhint('What link text should the user click on to open this file?', this, event, '200px')">[?]</a></td>

      <td><strong>Mark for Deletion</strong>  <a href="#" class="hintanchor" onMouseover="showhint('If you want this file to no longer associate with this document, you can mark the file for deletion; the file will be deleted from the server', this, event, '200px')">[?]</a></td>

          <td><strong>Press Release</strong>  <a href="#" class="hintanchor" onMouseover="showhint('Mark this file as a Press Release', this, event, '200px')">[?]</a></td>

         

     </tr>

     

     

    <!--- loop through files called by query getFiles ---> 

     <cfloop query="getFiles">

     

     

	  

	 <cfoutput>

     <input type="hidden" name="numberFiles" value="#getFiles.recordCount#">

          

     <cfif CurrentRow MOD 2 IS 1>

        <cfset bgcolor="##ffffff">

        <cfelse>

        <cfset bgcolor="##ebebeb">

      </cfif>

      

     <tr style="background-color:#bgcolor#">



        <!---  get list of file IDs --->  

       <td>#getFiles.FileID#</td>







        <!---  get list of file names --->  

       <td>#getFiles.FileName#</td>

       



        <!--- input field to update file link text --->   

        

        <cfset CTR = getFiles.currentRow>

          

       <td><cfinput type="Text"

            name="updateFileLinkText#CTR#"

            value="#getFiles.FileLinkText#"

            size="50"

            maxlength="100" />

            <input type="hidden" name="fileID#CTR#" value="#getFiles.fileID#" />

       </td>

               

      

         <!--- Mark For Deletion checkbox --->      

       <td><input type="checkbox" name="deleteFileID" value="#getFiles.fileID#" class="border0" /></td>



     

       <td>

       

   <!--- isPressRelease checkbox --->  

   

   <!--- 

      Give isPressRelease checkboxes name "isPressRelease". Give value #fileID# to checkboxes:



 --->  



    <input type="checkbox" name="isPressRelease" value="#fileID#" 

                         <cfif getFiles.isPressRelease>checked</cfif>>



      

</td>



</tr>



   

     </cfoutput>



<!--- close loop through files called by query getFiles ---> 

     </cfloop>

   </table>



  <cfelse>

   <p><strong>At the moment, no files are attached to this document.</strong></p>

  </cfif>



<p><strong>Upload a <em>new</em> file: PDF, TEXT, DOC, DOCX, XLS, XLXS, or image files only:</strong> <a href="#" class="hintanchor" onMouseover="showhint('Browse your local computer disk to find a file to upload', this, event, '200px')">[?]</a>



  <!--- input field for file upload --->

  



<cfinput type="file" size="50" accept="application/msexcel,application/msword,application/pdf,image/gif,image/jpeg,image/x-png" name="FileName" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

			</p>

            

        <!--- input field for file link text --->      

   <p><strong>Enter File Link Text:</strong> <a href="#" class="hintanchor" onMouseover="showhint('What link text should the user click on to open this file?', this, event, '200px')">[?]</a><br />



   <cfinput type="Text"

            name="FileLinkText"

            value=""

            size="50"

            maxlength="100" /></p>

       

            

            

           

           <!--- BEGIN ALIGN CENTER--->

   <div class="align-center">

   

   

   <!--- submit form to ColdFusion for processing; this is the DoSave function, which will add or edit a record --->

  <cfoutput>

    <input name="doSave" type="submit" value="#ButtonText#" class="btn" onMouseOver="this.className='btn btnhov'" onMouseOut="this.className='btn'" /> 

  </cfoutput>

  

  <span class="hintanchortext"><a href="#" class="hintanchor" onMouseover="showhint('Click the Update This Document button immediately left to apply your edits to this document', this, event, '200px')">[?]</a></span>

  

   

   

   

<cfif val(form.documentID)>



<hr />



<div class="graybox align-center width250px">

  <p>The button below deletes this document, its database record, and its associated files. Use deliberately and with caution.</p>

</div>





<!--- submit form to ColdFusion for processing; this is the DoDelete function, which deletes a Document record --->

<cfoutput>

  <input name="doDelete" type="submit" value="Delete This Document" class="btn" onMouseOver="this.className='btn btnhov'" onMouseOut="this.className='btn'" onclick="return confirm('Are you sure you wish to delete this document? After you delete it it is gone forever.')" />

</cfoutput>







<!--- END ALIGN CENTER --->

</div>

  



<cfelse>



</cfif>





<!--- END FORM --->

</cfform>





<!--- Page footer --->

<cfinclude template="/cep/cep_footer.cfm" />

Open in new window

0
Comment
Question by:Eric Bourland
  • 22
  • 14
  • 5
  • +2
44 Comments
 
LVL 11

Expert Comment

by:mattibutt
ID: 33642058
the database field should accept null also the form field shouldnt be applied any validation
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33642082
The database column DocumentImage does accept NULL.

I don't think there is any validation applied to this form field?

<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

0
 
LVL 36

Expert Comment

by:SidFishes
ID: 33642201
checking the form input should work...

<cfif form.DocumentImage neq "">
do your image processing
</cfif>
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 33642212
or rather

<cfif form.DocumentImage neq "">
<cffile action="upload"...
</cfif>
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33642214
Sid I will try that. Be right back...
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33642305
Sid, that solved one problem. I have a new error:

Element CLIENTFILE is undefined in CFFILE.  
 
126 :                             DocumentAbstract = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,
127 :                               DocumentKeyword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,
128 :                     DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">,
129 :                             DocumentPublicationDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,
130 :                             DateRecordModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

... but, I thought that ClientFile was a valid CFFILE upload variable?

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Expressions_7.html

That is I thought ClientFile would always be defined within tag CFFILE?
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33642333
More information:

The upload variable #cffile.clientFile#" should communicate the file name to the DocumentImage column in the database.

This works -- but only when I browse for a file and submit the form with a file selected in this CFINPUT: <cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

... so, the problem is (as I imagine you already know) =) ... how do I get ColdFusion to accept an empty value for variable #cffile.clientFile#"?

Is <cfif form.DocumentImage neq ""> still useful here? I am trying it.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 357 total points
ID: 33642815
>> #cffile.clientFile#"

First, I think don't think you want to use "clientFile".  That's the name on the USER's computer.  I'm pretty sure you want the name of the file saved on your *server*.  (They're not necessarily the same).  So use cffile.serverFile instead.


>> how do I get ColdFusion to accept an empty value for variable #cffile.clientFile#"

Second, #cffile.clientFile# will only be defined IF you run the "image processing ..." code.  So you need to create another variable, that always exists.  Give it a default value of "". Then update it only IF a file was uploaded. Finally use the new variable in your query instead.

<cfset uploadedImage = "">
<cfif form.DocumentImage neq "">
   .... do your image processing
  <cffile action="upload" ....>
   <cfset uploadedImage  = cffile.serverFile>
</cfif>

<cfquery ...>
UPDATE tbl_CEP_Documents
SET   DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,
....      

 DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#" null="#not len(uploadedImage)#">    ,

...
</cfquery>

0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33643243
_agx_,

Thank you! I am working on this. More in a while. Eric
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33644027
_agx_

I am making progress. I have learned

>>>First, I think don't think you want to use "clientFile".  That's the name on the USER's computer.

This confused me a little, because I use "clientFile" when I upload a PDF or a DOC file using the File Upload function a little further down in the application. But I will go with it and see how it works.

In this part here, I see that you defined variable uploadedImage twice ....

<cfset uploadedImage = "">
<cfif form.DocumentImage neq "">
   .... do your image processing
  <cffile action="upload" ....>
   <cfset uploadedImage  = cffile.serverFile>
</cfif>

.... but they are two different instances of uploadedImage; one that occurs if a file is uploaded, the other, if no file is uploaded. That makes sense.

So, I set it up like this -- see below, in the code snippet.

This code got rid of the "Element CLIENTFILE is undefined in CFFILE." error ... which makes sense.

And the image file upload does work ... I see new files as they are uploaded on the server into folder c:\upload\cep-dc.org.

However, the database table tbl_CEP_Documents does not get updated. When I submitted the form, and left blank the file upload input field (around line 612):

<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

... then the column DocumentImage is returned to a NULL value.

So ColdFusion thinks that uploadedImage = "".

Am I making sense? It is late. I am rambling.

When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. And if I browse for an image file and submit the form, then the image file does in fact upload ... but the DocumentImage column value is still NULL.

I wonder if I need to adjust:

<cfset uploadedImage = "">
<cfif form.DocumentImage neq "">
   .... do your image processing
  <cffile action="upload" ....>
   <cfset uploadedImage  = cffile.serverFile>
</cfif>

Also I still wonder if cffile.clientFile would work?

I am going to work on this further tomorrow. Thank you as always for your help. Have a good night.

Eric
<cfif val(form.DocumentID)>

                      

            <!--- group queries in a cftransaction: UPDATE Document; DELETE Document Topics; INSERT (new) Document Topics--->

			<cftransaction>

         

         

         <!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

				<cfset uploadedImage = "">

					<cfif form.DocumentImage neq "">



         

	<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

    	<!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

         

         

           <!--- DELETE DocumentImage --->

    

    	   <!--- begin CFELSEIF isdefined(form.deleteDocumentImage) --->

		 <cfelseif isDefined("form.deleteDocumentImage")>        

   

	<!---- query to delete DocumentImage for selected DocumentID ---->

    	   <cfquery name="qry_deleteDocumentImage" datasource="#ds#">

       			DELETE FROM tbl_CEP_Documents

           		WHERE DocumentImage = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

      		</cfquery>

         

           <cfset uploadedImage  = cffile.serverFile>

					</cfif>

			<!----- END form.DocumentImage neq "" ---->



    

  			  <cfquery name="UpdateDocument" datasource="#ds#">

				  UPDATE tbl_CEP_Documents

				  SET   DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

					DocumentType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

					DocumentAuthor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

				    DocumentAbstract = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  				    DocumentKeyword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                    DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#" null="#not len(uploadedImage)#">,

				    DocumentPublicationDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

				    DateRecordModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				  WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			</cfquery>





<cfquery name="UpdateDocumentDeleteTopics" datasource="#ds#">

   DELETE FROM tbl_Document_Has_Topic

   WHERE DocumentID = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

</cfquery>



<cfquery name="UpdateDocumentInsertNewTopics" datasource="#ds#">

INSERT INTO tbl_Document_Has_Topic

		(

        DocumentID

        ,  DocumentTopicID

        )

SELECT   <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

         , DocumentTopicID

FROM     tbl_CEP_Document_Topic

WHERE   DocumentTopicID  IN

(

<cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

)

</cfquery>

</cftransaction>







    <!--- CFELSE: DocumentID does not exist, then insert new document --->

				<cfelse> 

                

                

      <!--- use two queries: one to insert the record into tbl_CEP_Documents; one to insert DocumentID and DocumentTopicID into tbl_Document_Has_Topic  --->

      <!--- use cftransaction so both queries are processed together  --->



        <cftransaction>

	

				<!---- query to insert new document record into tbl_CEP_Documents ---->

					<cfquery name="InsertDocument" datasource="#ds#" result="newDocument">

				 INSERT INTO tbl_CEP_Documents

     					(

			            DocumentTitle,

            		    DocumentType,

                		DocumentAuthor,

		                DocumentAbstract,

                        DocumentKeyword,

                        DocumentImage,

        		        DocumentPublicationDate,

                		SSMA_TimeStamp

		                )

			     VALUES(

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                          <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#" null="#not len(uploadedImage)#">,

						  <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

		     			  <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				         )         

					</cfquery>

                  

    <!--- cfquery to get the selected document topics, and insert them into tbl_Document_Has_Topic along with the document ID --->

            <cfquery name="InsertDocumentTopics" datasource="#ds#">

                 INSERT INTO tbl_Document_Has_Topic

                 		(

                        DocumentID,

                        DocumentTopicID

                        )

                  SELECT

                   <cfqueryparam value="#newDocument.IDENTITYCOL#" cfsqltype="cf_sql_integer">

                   , DocumentTopicID

                   FROM  tbl_CEP_Document_Topic

                   WHERE   DocumentTopicID  IN

                  (

                     <cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

                  )

               </cfquery>

        </cftransaction>     

             

                  

                    

	 <!--- use the result attribute value to set the form field value --->

      <cfset form.DocumentID = newDocument.IDENTITYCOL>



		<!--- END queries to update or insert database records ---> 



        <!--- END cfif val(form.DocumentID) -- if a document needed to be updated, or added, then it was done --->

					    </cfif>

Open in new window

0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 357 total points
ID: 33647263
I haven't reviewed the code closely.  But I suspect the structure of the cfif's is messing things up.  (A common problem whenever you've got large blocks of code.)   It looks like you're saving #uploadedImage# in the wrong spot. You should capture the cffile.serverFile value directly after the <cffile...> upload.  

            <cfset uploadedImage = "">
            <cfif form.DocumentImage neq "">
                  <cffile action="upload" ....>
         
             <cfelseif isDefined("form.deleteDocumentImage")>        
                        ... do some delete ...
                         <!--- shouldn't be here. this should be after the upload --->
                       <cfset uploadedImage  = cffile.serverFile>
               </cfif>

>> <cftransaction>

BTW: Only the db queries should be inside the transaction.  All other code (file uploads, etc..) should be done _outside_ the transaction.  Otherwise, you're holding open the transaction for longer than is needed.  Transactions can block other operations. So the general rule is: always kept them short and sweet.






0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33649161
Got it.  I am looking closely at the order of the CFIFs. And I was wondering about the transaction. I will get back to you soon. Thank you. Hope your day is going well. EB
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33652115
_agx_,

Here is what I have found, after a lot of experimenting.

1) I put the CFTRANSACTION back in the correct place
2) I think my CFIFs are in order -- I have looked very closely at the order of the CFIFs
3) When I use cffile.clientFile, the application works properly; I can upload an image file, and the file then appears properly in the insert_update.cfm page, and there are no errors.
4) When I use cffile.serverFile, the application works mostly properly; I can upload an image file, but the file does NOT appear in the insert_update.cfm page ...
5) ... meaning, when I query for the uploaded image file placed in column DocumentImage, the image file appears if I use cffile.clientFile in my code (as below), but not if I use cffile.serverFile

However, you say that I should use cffile.serverFile. So naturally I am concerned that I am missing something!

I still need to create the code to delete the DocumentImage, and I will work on that next.

Do you have any idea why cffile.clientFile works but not cffile.serverFile?

I hope you are well and enjoying the evening.

Eric
<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

				<cfset uploadedImage = "">

                

 				<cfif form.DocumentImage neq "">      

                   

	<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

    	<!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

 

           <cfset uploadedImage  = cffile.clientFile>

           

           

					</cfif>

			<!----- END form.DocumentImage neq "" ---->

Open in new window

0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33652208
Hi Eric! i think most experts are helping you here, but as a safety reasons, u can try this!

i just added the form scope against your and try to insert into the database and see if it works!



<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->
<cfif form.DocumentImage neq "">
  <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->
  <!--- this is a document thumbnail image --->
  <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite" accept="image/*">
  <cfset form.uploadedImage  = #cffile.ServerFile#>
  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.ClientFileExt) eq 0>
    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">
  </cfif>
  <cfelse>
  <cfset form.uploadedImage = "">
</cfif>
<!----- END form.DocumentImage neq "" ---->
 and it will work! i hope
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33652211
and u said before u get an error! what kind of error u are getting!
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33655095
Hi, myselfrandhawa,

Good to hear from you and thanks for your note. Actually, at the moment I do not get an error. I am a little confused only because _agx_ advised me to use the CFFILE upload variable, cffile.serverFile. I have found that variable cffile.clientFile works better. But I also know that _agx_ does things for a reason. So I am curious to find out if cffile.serverFile can indeed work correctly, and if I have misunderstood or am doing something wrong.

The image file upload to column DocumentImage works correctly, using upload variable cffile.clientFile.

I am next working on a Delete function ... and I hope to get time to do that tomorrow (Sunday, Chicago time).

I hope you are well, and enjoying the weekend. Peace.

Eric B
0
 
LVL 15

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 143 total points
ID: 33655965
for a delete functionality!

you can always do this, pass the value of the previous file in your upload form, if it is empty, the following code will not executed, if not, it will check for its path and delete it!:


<cfif isDefined('form.previousfile')>
              <cfif FileExists("c:\project1\companies\#form.ID#\#form.previousfile#")>
                      <cffile action="delete" file="c:\project1\companies\#form.ID#\#form.previousfile#">            
            </cfif>
            </cfif>


So this way it will clean up your previous files.  as far the two functions you are comparing about! You can check the cfquickdocs.com website and see the difference, both works well.

Regards

0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33658372
myselfrandhawa,

Thank you for this. I am studying this and trying it out. I'll come back here later. I hope you are well.

Eric
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33658841
Eric,

>>> I am a little confused only because _agx_ advised me to use the CFFILE upload variable,
>>> cffile.serverFile. I have found that variable cffile.clientFile works better. But I also know that
>>> _agx_ does things for a reason. So I am curious to find out if cffile.serverFile can indeed work
>>? correctly, and if I have misunderstood or am doing something wrong.

Personally, I've NEVER used the client variables.  The documentation says they are used to return information about the _client_ (or user) system and NOT the server. So I seriously doubt they're what you should be using.  

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_02.html
clientFile        - Name of the file uploaded from the CLIENT's system
serverFile - Filename of the file SAVED on the server

I strongly suspect the problem is something else.  But .. that said, I haven't tested your code.
If you're still having problems I would suggest testing the code separately.  In other words, a new page with nothing except the section you're trying to test.  So you can rule out other sections causing problems with your code.



0
 
LVL 52

Expert Comment

by:_agx_
ID: 33658846
>> So I seriously doubt they're what you should be using.  

...  assuming you want to the name of the file saved on your server that is.
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33659575
yes please either paste you whole code here or just sattach it, so i can test it and see what is wrong with ur code
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33665266
_agx_ and myselfrandhawa,

I realize that maybe I have not communicated clearly. I'll try to explain better what I am doing.

The parameter "clientFile" is used for the "Name of the file uploaded from the client's system", per this note here:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_02.html

... and I believe that Name of the file uploaded from the client's system" is exactly what I want. Unless I have misunderstood my own needs, or misunderstood your recommendation or advice (always very possible that I have misunderstood).

So, here is how the application should work -- and, again, I should have spelled out this process more carefully to begin --

1. the end-user -- an authorized person who works at CEP browses on her computer for an image file to upload
2. that image file has a name and it is OK to keep that name
3. she uploads the file to folder c:\uploads\cep-dc.org
4. the original file name is still OK ... I believe that this original file name fits the parameter "clientFile" specified by ColdFusion; this file name is the "Name of the file uploaded from the client's system"

... and I ~think~ this is all OK. There are no errors and the image file and the file upload function seem to be working correctly.

What I am afraid of is -- and I am always afraid of this -- that I have miscommunicated or misunderstood. _agx_ recommends using parameter serverFile, which is the "Filename of the file saved on the server"

In this case, I think that serverFile and clientFile have the same value.

For purposes of uploading a file, is it OK to set variable uploadedImage to have the value cffile.clientFile?

<cfset uploadedImage  = cffile.clientFile>

Thanks for your patience while I make myself undestand this.

Eric
<cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

 

           <cfset uploadedImage  = cffile.clientFile>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:Eric Bourland
ID: 33665602
_agx_ and myselfrandhawa,

Also, I am following up on myselfrandhawa's note about deleting the thumbnail image. I think I have set up the code properly to delete a document thumbnail image.

      <!--- if the Delete Document Thumbnail Image checkbox is checked, then delete the document thumbnail image --->  
<cfif isDefined('form.deleteDocumentImage')>
              <cfif FileExists("c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>
                      <cffile action="delete" file="c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">            
            </cfif>
            </cfif>

I am running tests on this code now.... so far, no errors. I am checking to see if the file is actually deleted from the server, and if the file name is actually deleted from column DocumentImage in tbl_CEP_Documents.

I hope your day is going well! =)

Eric
here is the code that manages the form field DocumentImage; it includes code to upload a file, and code to delete a file:



    <!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

				<cfset uploadedImage = "">

                

 				<cfif form.DocumentImage neq "">      

                   

	<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

    	<!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

 

           <cfset uploadedImage  = cffile.clientFile>

           

           

					</cfif>

			<!----- END form.DocumentImage neq "" ---->







    	<!--- if the Delete Document Thumbnail Image checkbox is checked, then delete the document thumbnail image --->   

<cfif isDefined('form.deleteDocumentImage')>

              <cfif FileExists("c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>

                      <cffile action="delete" file="c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">            

            </cfif>

            </cfif>







.... and, further down in the application, here is the checkbox that a user checks in order to specific "Delete the document thumbnail image":



<input type="checkbox" name="deleteDocumentImage" value="#getDocumentImage.DocumentImage#" class="border0" />

Open in new window

0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33666077
OK. Sorry to flood this thread with information. I have new information.

1) I have found that the DocumentImage upload code messes up the other upload code for Files -- that is, PDF and Doc files that are associated with the DocumentID.

2) So, I moved the DocumentImage code to the very end of the DoSave ... so that, when the form is submitted, the very last function that ColdFusion processes is the DocumentImage upload function.

3) But now I find that I get the same result whether or not I use the serverFile or clientFile parameter.

4) Following _agx_'s advice, I will proceed to use the cffile.serverFile parameter going forward ... I think it should be fine .... because both parameters serverFile and clientFile contain the same value

5) now my problem is this: When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. Even if there were a value specififed there, before.

So, this code, below, sets column DocumentImage to NULL every time I submit the form.

That is the problem I am currently working on.

Again, sorry to flood this thread with updates. I am trying to give very clear information. Thanks again for any advice.

Should I show you the entire application? I hesitate only because I do not want to ask you to sort through all of that code. =) The document image code begins on line 361.

Eric
<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

				<cfset uploadedImage = "">

             

 				<cfif form.DocumentImage neq "">      

                   

	<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

    	<!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

			</cfif>

 

           <cfset uploadedImage  = cffile.serverFile>

           

           

					</cfif>

			<!----- END form.DocumentImage neq "" ---->

Open in new window

0
 
LVL 15

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 143 total points
ID: 33668798
Hi Eric! i had made your code so simple that you need to take tension about the values here and there:

<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->
            <cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">      
                   
      <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->      
          <!--- this is a document thumbnail image --->      
            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">
            <cfset uploadedImage  = #cffile.serverFile#>
                  
                  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>
                            <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">
                  </cfif>
            <cfelse>
            <cfset uploadedImage = "">        
                  </cfif>
                  <!----- END form.DocumentImage neq "" ---->
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33668874
myselfrandhawa,

I will try it out now! Thank you. I'll be back soon.

Eric
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33669117
myselfrandhawa,

Hmmm. Well, the problem is still there:  When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. Even if there were a value specififed there, before.

Something is going on to set the value in the DocumentImage column to NULL. I am guessing it must be:      <cfset uploadedImage = "">      

... but uploadedImage is set to "" only if: cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq ""

So I am confused.

I attach my full code, below. Here are some notes to help:

* The cffile action="upload" part begins around line 57, right after Form.DoSave is defined

* The Document Thumbnail Image browse input field begins around line 602

I am sorry to ask you to sift through all of that code. What do you think is going on, to set the value in the DocumentImage column to NULL?

Thank you as always for your help. I hope you are well.

Eric
<!---

Name:        insert_update.cfm

Author:      Eric B, following Ben Forta code in ColdFusion 8 web application construction kit; gdemaria, _agx_, azadi, Plucka, myselfrandhawa, Brichsoft

Description: CEP add and update documents administration

Created:     2/24/2010 - 8/23/2010

ColdFusion Version 8

MS SQL Server 2005

--->





 <!--- Set default value for FileID in scope FORM --->

<cfparam name="form.FileID" default="">



 <!--- Set default value for NumberFiles in scope FORM --->

<cfparam name="form.NumberFiles" default="">



 <!--- Set default value for SelectDocumentTopics in scope FORM --->

<cfparam name="form.SelectDocumentTopics" default="0">





 <!--- Set default value for DocumentID in scope URL --->

<cfparam name="URL.DocumentID" default="">



 <!--- Define DocumentID in scope FORM, then set form.DocumentID equal to the DocumentID passed in the URL: for use later in the application --->

<cfparam name="form.DocumentID" default="#URL.DocumentID#">







<!--- default isPressRelease is 0 (false) --->

<cfparam name="form.isPressRelease" default="0">









 <!--- Set datasource --->

 <cfset ds="ebwebwork">

  

 <!---- begin CFTRY; catch any errors, whether you throw them or the database does; and to test that file were uploaded successfully, or not  ---->

 <cftry>  

 



<!---- populate this with an error message ---->

<cfset variables.error = ""> 





        <!--- BEGIN: Save action --->





<!--- begin form.doSave --->



<!--- when the user clicks the Save Button, these events can occur: Add a document; Update a document; Delete a Document; Delete a file that is associated with a document --->



<cfif IsDefined("FORM.doSave")>







<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

            <cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">      

                   

      <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

          <!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

            <cfset uploadedImage  = #cffile.serverFile#>

                  

                  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

                            <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

                  </cfif>

            <cfelse>

            <cfset uploadedImage = "">         

                  </cfif>

                  <!----- END form.DocumentImage neq "" ---->

                  





<!--- make sure that documentTitle and documentType are entered --->  

    

	<cfif len(form.DocumentTitle) eq 0>

			  <cfthrow message="Document Title is required">

	</cfif>

    

    <cfif len(form.DocumentType) eq 0>

			  <cfthrow message="Document Type is required">

	</cfif>





 <!--- in this query select NOTHING from table tbl_CEP_documents, and simply check if DocumentTitle exists --->

 

 <cfquery datasource="#ds#" name="CheckDocumentTitle">

  SELECT 'Nothing' FROM tbl_CEP_Documents

  WHERE DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.DocumentTitle#">

  AND DocumentID <> <cfqueryparam cfsqltype="cf_sql_integer" value="#val(Form.DocumentID)#">

  </cfquery>

   

 

  

   <!--- if DocumentTitle exists, display error; refuse record insert --->

   

	<cfif CheckDocumentTitle.recordcount GT 0>

		   <cfthrow message="The Document Title is already taken; please enter another title">

	</cfif>

    

    <!--- begin statements to update, insert database records --->  

   

    <!--- begin cfif val(form.DocumentID) --->

    

    <!--- the action is UPDATE; a DocumentID Exists --->

    

				<cfif val(form.DocumentID)>

                               

      



   <!--- group queries in a cftransaction: UPDATE Document; DELETE Document Topics; INSERT (new) Document Topics--->

			<cftransaction>

    

  			  <cfquery name="UpdateDocument" datasource="#ds#">

				  UPDATE tbl_CEP_Documents

				  SET   DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

					DocumentType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

					DocumentAuthor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

				    DocumentAbstract = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  				    DocumentKeyword = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                    DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#" null="#not len(uploadedImage)#">,

				    DocumentPublicationDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

				    DateRecordModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				  WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			</cfquery>





<cfquery name="UpdateDocumentDeleteTopics" datasource="#ds#">

   DELETE FROM tbl_Document_Has_Topic

   WHERE DocumentID = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

</cfquery>



<cfquery name="UpdateDocumentInsertNewTopics" datasource="#ds#">

INSERT INTO tbl_Document_Has_Topic

		(

        DocumentID

        ,  DocumentTopicID

        )

SELECT   <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

         , DocumentTopicID

FROM     tbl_CEP_Document_Topic

WHERE   DocumentTopicID  IN

(

<cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

)

</cfquery>

</cftransaction>







    <!--- CFELSE: DocumentID does not exist, then insert new document --->

				<cfelse> 

                

                

      <!--- use two queries: one to insert the record into tbl_CEP_Documents; one to insert DocumentID and DocumentTopicID into tbl_Document_Has_Topic  --->

      <!--- use cftransaction so both queries are processed together  --->



        <cftransaction>

	

				<!---- query to insert new document record into tbl_CEP_Documents ---->

					<cfquery name="InsertDocument" datasource="#ds#" result="newDocument">

				 INSERT INTO tbl_CEP_Documents

     					(

			            DocumentTitle,

            		    DocumentType,

                		DocumentAuthor,

		                DocumentAbstract,

                        DocumentKeyword,

                        DocumentImage,

        		        DocumentPublicationDate,

                		SSMA_TimeStamp

		                )

			     VALUES(

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,

						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,

  						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentKeyword)#">,

                          <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#" null="#not len(uploadedImage)#">,

						  <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,

		     			  <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">

				         )         

					</cfquery>

                  

    <!--- cfquery to get the selected document topics, and insert them into tbl_Document_Has_Topic along with the document ID --->

            <cfquery name="InsertDocumentTopics" datasource="#ds#">

                 INSERT INTO tbl_Document_Has_Topic

                 		(

                        DocumentID,

                        DocumentTopicID

                        )

                  SELECT

                   <cfqueryparam value="#newDocument.IDENTITYCOL#" cfsqltype="cf_sql_integer">

                   , DocumentTopicID

                   FROM  tbl_CEP_Document_Topic

                   WHERE   DocumentTopicID  IN

                  (

                     <cfqueryparam value="#form.SelectDocumentTopics#" cfsqltype="cf_sql_integer" list="true">

                  )

               </cfquery>

        </cftransaction>     

             

                  

                    

	 <!--- use the result attribute value to set the form field value --->

      <cfset form.DocumentID = newDocument.IDENTITYCOL>



		<!--- END queries to update or insert database records ---> 



        <!--- END cfif val(form.DocumentID) -- if a document needed to be updated, or added, then it was done --->

					    </cfif>  

    

    

    

	   <!--- ============= BEGIN query to update column FileLinkText in tbl_CEP_Files ============= --->   

              <!--- this section must be placed within the Save Action --->  

 

        <cfloop index="kk" from="1" to="#val(form.numberFiles)#"> 

            <cfset form.updateFileLinkText = form['updateFileLinkText' & kk]>

            <cfset form.fileID = form['fileID' & kk]>

        

             <cfquery name="query_updateFileLinkText" datasource="#ds#">

                UPDATE tbl_CEP_Files

                SET FileLinkText = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.updateFileLinkText)#">

                WHERE FileID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.fileID)#">

             </cfquery>            

         

        </cfloop>

     

        <!--- ============= END query to update column FileLinkText in tbl_CEP_Files ============= --->   

    



    

<!--- remember cfif IsDefined("FORM.doSave") is still defined -- remember to close the cfif later --->

    

  

				<!--- test to see whether or not a file is being uploaded -- does form field FileName contain a file? --->         

  

		    	<!--- BEGIN: if form field FileName contains a file, then upload the file and the file's metadata --- this function updates the tbl_CEP_Files--->

      		<cfif form.fileName is not "">

     

              

			    <!--- BEGIN: upload a file using CFFILE --->       

     			<!--- cffile upload --->       

   			      <cffile action="upload" filefield="FileName" destination="c:\upload\cep-dc.org" nameconflict="overwrite">



			<cfif listFindNoCase("doc,docx,jpg,jpeg,png,gif,pdf,ppt,xls,xlsx,txt", cffile.serverFileExt) eq 0>

				    <cfthrow message="Your file did not upload. You may upload only permitted file types.">

			</cfif>

            



			<!---- query to insert new record into tbl_CEP_Files, with a file  (DOC, JPEG, PDF, etc.) to upload ---->

			<cfquery name="InsertFile" datasource="#ds#">

			 INSERT INTO tbl_CEP_Files

     			(

                DocumentID,

                FileName,

                FileLinkText,

				FileExtension,

				FileType,

				FileSize,

                isDeleted,

                isPressRelease

                )

		     VALUES(

        		  <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">

                  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.FileLinkText)#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFileExt#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.contentType#">

				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.fileSize#">

                  ,0

                  ,0

		           )         

				</cfquery>  

  

		   <!--- END query to insert new record into tbl_CEP_Files --->  

 

 

     

              <!--- DELETE A FILE --->

      <!--- this query must be placed within the Save Action --->

 

    

    	   <!--- begin CFELSEIF isdefined(form.deleteFileID) --->

		 <cfelseif isDefined("form.deleteFileID")>        

   

	<!---- query to set a file to isDeleted = 1 ---->

    	   <cfquery name="qry_deleteFile" datasource="#ds#">

       			UPDATE tbl_CEP_Files

           		SET IsDeleted = 1

        		WHERE FileID IN (<cfqueryparam cfsqltype="cf_sql_integer" value="#form.deleteFileID#" list="yes">)

      		</cfquery>





    <!--- SET OR UNSET A FILE TO isPressRelease --->

      <!--- this query must be placed within the Save Action --->

      

      <!--- Use #form.documentID# to identify all files associated with the current document --->

 

    	   <!--- begin CFELSEIF isDefined(form.isPressRelease) --->

           <!--- use #form.isPressRelease# to exclude checked files --->

		 

		 

		 <cfelseif isDefined("form.isPressRelease")>

   

   <cftransaction>

   <!---- query to handle unchecked files (set isPressRelease = 0---->

  <cfquery name="uncheckedFiles" datasource="#ds#">

            UPDATE      tbl_CEP_files

            SET     isPressRelease = 0

            WHERE      documentID = <cfqueryparam value="#form.documentID#" cfsqltype="cf_sql_integer"> 

            <cfif listLen(form.isPressRelease)>

            AND            fileID NOT IN 

                        (

                              <cfqueryparam value="#form.isPressRelease#" list="true" cfsqltype="cf_sql_integer">

                        )

            </cfif>

      </cfquery>







	<!---- query to handle checked files (set isPressRelease = 1) ---->



      <cfif listLen(form.isPressRelease)>

            <cfquery name="checkedFiles" datasource="#ds#">

            UPDATE      tbl_CEP_files

            SET     isPressRelease = 1

            WHERE      fileID IN 

                        (

                              <cfqueryparam value="#form.isPressRelease#" list="true" cfsqltype="cf_sql_integer">

                        )

            </cfquery>

      </cfif>

      

</cftransaction>





            </cfif>

<!----- END form.fileName is not "" test to see if file is being uploaded ---->







	<!--- if insert record succeeded, and / or file upload succeeded, and / or FileLinkText were updated, then go to index page --->

				     <cflocation url="index.cfm" addtoken="no">



        

                    

    <!--- END: Save action --->

    



  

        <!--- BEGIN: Delete action --->





			   <!--- delete a database record --->

					   <!--- begin CFELSEIF isdefined(form.doDelete) --->

					        <cfelseif isDefined("form.doDelete")>        

					        <!---- query to delete record ---->

                            

                            <cftransaction>

					       	<cfquery name="DeleteDocument" datasource="#ds#">

					         DELETE FROM tbl_CEP_Documents

					         WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

					        </cfquery>

                                                        

                            <cfquery name="UpdateDocumentDeleteTopics" datasource="#ds#">

							   DELETE FROM tbl_Document_Has_Topic

							   WHERE DocumentID = <cfqueryparam value="#form.DocumentID#" cfsqltype="cf_sql_integer">

							</cfquery>

                            </cftransaction>

              

 					        <!---- after delete, go to index page ---->

						 <cflocation url="index.cfm" addtoken="no">



	    <!--- END: Delete action --->

        

  

                  

                  





  <!--- END form.doSave --->

</cfif>











       <!--- this CFCATCH will trap errors -- the ones you threw or just regular database issues --->

<cfcatch type="Any">

     <cfset variables.error = cfcatch.message>

     <cfrethrow>

</cfcatch>



  

  

  

		  <!--- END CFTRY --->  

			</cftry>







<!--- fetch the data from the database only when there are no errors.

      if an error exists, then let the form variables pass back into the form to display ---->

 

  <cfif len(variables.error) eq 0>

    

			  <!--- get data from table tbl_CEP_Documents and convert the data into form variables --->

			  <cfquery name="getDocumentDetails" datasource="#ds#">

				    select * from tbl_CEP_Documents where DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			  </cfquery>



  			<cfloop index="aCol" list="#getDocumentDetails.columnList#">

			       <cfset "form.#aCol#" = getDocumentDetails[aCol][getDocumentDetails.currentRow]>

			  </cfloop>

    

	</cfif>











<!--- BEGIN HTML / CSS PAGE HEADER --->

<cfinclude template="/cep/cep_header_admin.cfm" />









<!--- if there an error, display error in human readable form --->



<cfif len(variables.error)> 

			 <cfoutput>

			 <div style="border: 1px solid red; padding: 10px; margin:20px; width:400px;">#variables.error#</div>

			 </cfoutput>

</cfif>















				<!----- if record already exists (it will have an ID) then update it; otherwise, add new record... ----->

				<cfif val(form.documentID)>

					  <cfset FormTitle="Edit Document">

					  <cfset ButtonText="Update This Document">

  					  <cfset FileLinkTextButton="Update File Link Text">

				<cfelse>

						<cfset FormTitle="Add a Document">

						<cfset ButtonText="Add Document">



				</cfif>

                

                

  

				<!--- Add or Update Document Form begins here --->

				<cfform method="post" enctype="multipart/form-data" scriptsrc="#Request.CFFORM_JS_LIB#">





 <!--- Embed documentID (PK), fileID, numberFiles as hidden fields, to assign a value to each --->

 <cfoutput>

<input type="hidden" name="documentID" value="#form.documentID#" />



   </cfoutput>







  <cfoutput>



      <h2>#FormTitle#</h2>



  </cfoutput>

  

  

  <!--- BEGIN TABLE ADMIN; DISPLAY DOCUMENT DETAILS --->

 

<table class="table_admin">

<cfif val(form.documentID)>

 <tr>

  <td>

<p><strong>Document ID: <cfoutput>#URL.DocumentID#</cfoutput></strong></p>

  </td>

 </tr>

 <cfelse>

 </cfif>



 <tr>

  <td>

   <p><strong>Document Title</strong><br />



   <cfinput type="Text"

            name="DocumentTitle"

            value="#form.DocumentTitle#"

            message="Document title is required! Please restrict the Document title to 100 characters or fewer."

            required="Yes"

            validateAt="onSubmit,onServer"

            size="100"

            maxlength="250" /></p>

  </td>

 </tr>

 <tr>

  <td>

   <p><strong>Document Type</strong>  <a href="#" class="hintanchor" onMouseover="showhint('Don\'t worry too much about document type. Specify document type \'Report\' for most CEP documents.', this, event, '200px')">[?]</a></p>



   <select name="DocumentType" value="#form.DocumentType#">

   			<option value="Report">Report</option>

            <option value="Policy Brief">Policy Brief</option>

            <option value="Press Release">Press Release</option>

            <option value="Article">Article</option>

            <option value="Summary">Summary</option>

            <option value="Letter">Letter</option>

            <option value="Letter">Pamphlet</option>

            <option value="Letter">Information Bulletin</option>

            <option value="Audio Transcript">Audio Transcript</option>

            <option value="Text Transcript">Text Transcript</option>

   </select>

  </td>

 </tr>





 <tr>

  <td>

   <p><strong>Document Author</strong><br />



   <cfinput type="Text"

            name="DocumentAuthor"

            value="#form.DocumentAuthor#"

            message="Enter Document Author Name"

            required="yes"

            validateAt="onSubmit,onServer"

            size="100"

            maxlength="100" /></p>

  </td>

 </tr>

 

 

<!--- this query requests values from columns DocumentTopicID and DocumentTopic in tbl_CEP_Document_Topics --->

 <cfquery datasource="#ds#" name="GetDocumentTopics">

SELECT DocumentTopicID, DocumentTopic

FROM tbl_CEP_Document_Topic

ORDER BY DocumentTopic

  </cfquery>

 

  <tr>

  <td>

    <p><strong>Assign this Document to CEP Topics</strong> <a href="#" class="hintanchor" onMouseover="showhint('Assign this document to a topic', this, event, '200px')">[?]</a></p>

    

    

      <p class="small">Choose at least one topic. To select more than one topic, hold the Ctrl or the Shift key (on your keyboard) and select topics with the mouse cursor.</p>

   



<!--- query to get existing topics for the current documentID --->



       <cfquery datasource="#ds#" name="GetSelectedTopics">

              SELECT DocumentTopicID

              FROM    tbl_Document_Has_Topic

              WHERE  DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.documentID)#">

       </cfquery>

     

       <!--- convert the DocumentTopicID values to a list --->

      <cfset form.SelectDocumentTopics = ValueList(GetSelectedTopics.DocumentTopicID)>



<cfselect name="SelectDocumentTopics"

size="8"

multiple="yes"

query="GetDocumentTopics"

queryPosition="below"

value="DocumentTopicID"

display="DocumentTopic"

selected="#form.SelectDocumentTopics#">

     <option value="0">Choose topic:</option>

</cfselect>

 

  </td>

 </tr>

 



 <tr>

  <td>

   <p><strong>Document Abstract</strong><br />

   <cfoutput>

   <textarea name="DocumentAbstract" wrap="virtual" style ="width:600px; height:250px;">#form.DocumentAbstract#</textarea>

   </cfoutput></p>

  </td>

 </tr>

 

  <tr>

  <td>

   <p><strong>Document Keywords</strong> <a href="#" class="hintanchor" onMouseover="showhint('Enter keywords as a simple list, without punctuation.', this, event, '200px')">[?]</a><br />



   <cfinput type="Text"

            name="DocumentKeyword"

            value="#form.DocumentKeyword#"

            size="200"

            maxlength="255" /></p>

  </td>

 </tr>

 

 

  <!--- begin DocumentImage table row ---> 



  <tr>

  <td>

   <p><strong>Document Thumbnail Image</strong> <a href="#" class="hintanchor" onMouseover="showhint('Browse local computer for thumbnail image', this, event, '200px')">[?]</a><br />



<cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" /></p>



 

    <!--- query to get columns and values from tbl_CEP_files, for use below --->

<cfquery name="getDocumentImage" datasource="#ds#">

    SELECT DocumentImage

    FROM tbl_CEP_Documents

    WHERE DocumentID=<cfqueryparam value="#val(form.DocumentID)#" cfsqltype="cf_sql_integer"> 

</cfquery>





<!--- begin table to display the file called by query getDocumentImage ---> 



<cfif getDocumentImage.recordCount>

   <table class="tablecontrolpanel width600px">

     <tr>

      <td><strong>Thumbnail image file associated with this CEP document:</strong></td>

      <td><strong>Mark for Deletion</strong>  <a href="#" class="hintanchor" onMouseover="showhint('If you want this file to no longer associate with this document, you can mark the file for deletion; the file will be deleted from the server', this, event, '200px')">[?]</a></td>

     </tr>

     

	 <cfoutput>

      

     <tr>





        <!---  get list of file names --->  

       <td>#getDocumentImage.DocumentImage#</td>

       



         <!--- Mark For Deletion checkbox --->      

       <td><input type="checkbox" name="deleteDocumentImage" value="#getDocumentImage.DocumentImage#" class="border0" /></td>



     

    

</tr>



   

     </cfoutput>





   </table>



  <cfelse>

   <p><strong>At the moment, no thumbnail image files are attached to this document.</strong></p>

  </cfif>

<!--- end table to display the file called by query getDocumentImage ---> 







  </td>

 </tr>

 <!--- end DocumentImage table row; the table markup gets confusing; be careful ---> 







 

 <tr>

  <td>

   <p><strong>Publication Date (use form MM/DD/YYYY; please click the calendar icon, below)</strong><br />

   <cfinput name="DocumentPublicationDate"

            value="#DateFormat(DocumentPublicationDate, "mm/dd/yyyy")#"

            message="Publication Date must be a valid date!"

            required="no"

            validate="usdate"

            validateAt="onSubmit,onServer"

            size="10"

            maxlength="10"

            type="datefield" mask="MM/DD/YYYY" /></p>

  </td>

 </tr>

 </table>



  <!--- END TABLE ADMIN; DISPLAY DOCUMENT DETAILS --->

  







   <!--- display the files currently associated with the document --->



<cfif val(form.documentID)>

<p>Below, please see the files currently associated with Document ID <strong><cfoutput>#URL.DocumentID#</cfoutput></strong>. If there are no files associated with the document, then no files will appear below. You have the option to upload a file (PDF, DOC, image file), which will associate the file with this document.</p>

<cfelse>

<p>This is a new document. You have the option to upload a file (PDF, DOC, image file), which will associate the file with this document. Large files will take some time to upload; please be patient.</p>

</cfif>





   <!--- query to get columns and values from tbl_CEP_files, for use below --->

<cfquery name="getFiles" datasource="#ds#">

    SELECT *

    FROM tbl_CEP_files

    WHERE DocumentID=<cfqueryparam value="#val(form.DocumentID)#" cfsqltype="cf_sql_integer"> 

    AND isDeleted = 0

  </cfquery>

  



  <cfif getFiles.recordCount>

   <table class="tablecontrolpanel width600px">

     <tr>

      <td><strong>File ID</strong></td>

      <td><strong>Files associated with this CEP document:</strong></td>

      <td><strong>File Link Text</strong> <a href="#" class="hintanchor" onMouseover="showhint('What link text should the user click on to open this file?', this, event, '200px')">[?]</a></td>

      <td><strong>Mark for Deletion</strong>  <a href="#" class="hintanchor" onMouseover="showhint('If you want this file to no longer associate with this document, you can mark the file for deletion; the file will be deleted from the server', this, event, '200px')">[?]</a></td>

          <td><strong>Press Release</strong>  <a href="#" class="hintanchor" onMouseover="showhint('Mark this file as a Press Release', this, event, '200px')">[?]</a></td>

         

     </tr>

     

     

    <!--- loop through files called by query getFiles ---> 

     <cfloop query="getFiles">

     

     

	  

	 <cfoutput>

     <input type="hidden" name="numberFiles" value="#getFiles.recordCount#">

          

     <cfif CurrentRow MOD 2 IS 1>

        <cfset bgcolor="##ffffff">

        <cfelse>

        <cfset bgcolor="##ebebeb">

      </cfif>

      

     <tr style="background-color:#bgcolor#">



        <!---  get list of file IDs --->  

       <td>#getFiles.FileID#</td>







        <!---  get list of file names --->  

       <td>#getFiles.FileName#</td>

       



        <!--- input field to update file link text --->   

        

        <cfset CTR = getFiles.currentRow>

          

       <td><cfinput type="Text"

            name="updateFileLinkText#CTR#"

            value="#getFiles.FileLinkText#"

            size="50"

            maxlength="100" />

            <input type="hidden" name="fileID#CTR#" value="#getFiles.fileID#" />

       </td>

               

      

         <!--- Mark For Deletion checkbox --->      

       <td><input type="checkbox" name="deleteFileID" value="#getFiles.fileID#" class="border0" /></td>



     

       <td>

       

   <!--- isPressRelease checkbox --->  

   

   <!--- 

      Give isPressRelease checkboxes name "isPressRelease". Give value #fileID# to checkboxes:



 --->  



    <input type="checkbox" name="isPressRelease" value="#fileID#" 

                         <cfif getFiles.isPressRelease>checked</cfif>>



      

</td>



</tr>



   

     </cfoutput>



<!--- close loop through files called by query getFiles ---> 

     </cfloop>

   </table>



  <cfelse>

   <p><strong>At the moment, no files are attached to this document.</strong></p>

  </cfif>



<p><strong>Upload a <em>new</em> file: PDF, TEXT, DOC, DOCX, XLS, XLXS, or image files only:</strong> <a href="#" class="hintanchor" onMouseover="showhint('Browse your local computer disk to find a file to upload', this, event, '200px')">[?]</a>



  <!--- input field for file upload --->

  



<cfinput type="file" size="50" accept="application/msexcel,application/msword,application/pdf,image/gif,image/jpeg,image/x-png" name="FileName" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

			</p>

            

        <!--- input field for file link text --->      

   <p><strong>Enter File Link Text:</strong> <a href="#" class="hintanchor" onMouseover="showhint('What link text should the user click on to open this file?', this, event, '200px')">[?]</a><br />



   <cfinput type="Text"

            name="FileLinkText"

            value=""

            size="50"

            maxlength="100" /></p>

       

            

            

           

           <!--- BEGIN ALIGN CENTER--->

   <div class="align-center">

   

   

   <!--- submit form to ColdFusion for processing; this is the DoSave function, which will add or edit a record --->

  <cfoutput>

    <input name="doSave" type="submit" value="#ButtonText#" class="btn" onMouseOver="this.className='btn btnhov'" onMouseOut="this.className='btn'" /> 

  </cfoutput>

  

  <span class="hintanchortext"><a href="#" class="hintanchor" onMouseover="showhint('Click the Update This Document button immediately left to apply your edits to this document', this, event, '200px')">[?]</a></span>

  

   

   

   

<cfif val(form.documentID)>



<hr />



<div class="graybox align-center width250px">

  <p>The button below deletes this document, its database record, and its associated files. Use deliberately and with caution.</p>

</div>





<!--- submit form to ColdFusion for processing; this is the DoDelete function, which deletes a Document record --->

<cfoutput>

  <input name="doDelete" type="submit" value="Delete This Document" class="btn" onMouseOver="this.className='btn btnhov'" onMouseOut="this.className='btn'" onclick="return confirm('Are you sure you wish to delete this document? After you delete it it is gone forever.')" />

</cfoutput>







<!--- END ALIGN CENTER --->

</div>

  



<cfelse>



</cfif>





<!--- END FORM --->

</cfform>





<!--- Page footer --->

<cfinclude template="/cep/cep_footer.cfm" />

Open in new window

0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 357 total points
ID: 33674361
>> then the value in the DocumentImage column is set to NULL. Even if there were a
>> value specififed there, before

If you ONLY want to update the value under certain conditions, you need to specify that in the sql.  Otherwise, you're overwriting the value every time.

UPDATE  .... etc.....
DocumentKeyword = <cfqueryparam .... >   ,

<!--- ONLY change the value IF something was uploaded ...--->
<cfif len(trim(uploadedImage))>
     DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#">,
</cfif>

DocumentPublicationDate = <cfqueryparam ....>,
....etc....
                  


>> I think it should be fine .... because both parameters serverFile and clientFile
>> contain the same value

Yes, but they won't ALWAYS be same.  That's why you should always use the server value, because that's the one that matters.  The current code is using nameconflict="overwrite"  (... I'm not sure that's a good idea) and that's why you're seeing the same value for both variables.  BUT, if you changed it to use nameconflict="makeunique",  the values won't always be the same.  So if you were still using cffile.clientFile you'd potentially end up storing the _wrong_ file name.
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33674772
>>>So if you were still using cffile.clientFile you'd potentially end up storing the _wrong_ file name

As I was inbed gradually waking up this morning, I was thinking about the differences between cffile.clientFile and cffile.serverFile. I understand now what you are saying.

>>>ONLY change the value IF something was uploaded

This is also making more sense. I am going to try this out now and will be back here soon. Thank you _agx_ =)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33675149
>> using nameconflict="overwrite"

Thinking about this some more ... you may want to rethink using overwrite.  It has advantages in some cases. But think about what would happen if two users uploaded a file with the same name, but for totally different parent records:

- Today, UserA uploads  "MyFile.doc" (20 KB) under "Parent Document A"
- The next day UserB uploads a totally different file (also named "MyFile.doc" - 16MB) under "Parent Document EFG"

UserB's file will overwrite User A's file. Now both "Parent Document A" and "Parent Document EFG" will point to UserB's file.




0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33675221
>> using nameconflict="overwrite"

I have been thinking about this too. I understand what you are saying. If two users upload a file that have the same name, then the first file will be overwritten. At first I thought: that is actuallyOK, given the needs and habits of this client. Then I went ahead and changed the nameconflict attribute from "overwrite" to "makeunique" .... that could be useful. And it might forestall confusion. I agree: this is a better idea.

This did the trick:

<cfif len(trim(uploadedImage))>
     DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedImage#">,
</cfif>


and it makes sense. I should have thought of that. ~sheepish~

I am next working on the delete function, using myselfrandhawa's idea:

   <!--- if the Delete Document Thumbnail Image checkbox is checked, then delete the document thumbnail image --->  
<cfif isDefined('form.deleteDocumentImage')>
              <cfif FileExists("c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>
                      <cffile action="delete" file="c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">            
            </cfif>
            </cfif>

Before, this code was interfering with the File Delete and File Press Release functions ... but I think I can work around that now. I'll come back here later with a progress report.

Thank you again and I hope your day is going well.

Eric
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33675653
>> I am next working on the delete function

Sorry I haven't caught up on the whole thread.  What is the delete function supposed to do:

A) Delete the database record for a file AND
B) Delete the physical file

?

Also, it is supposed to operate on multiple files at one time?

0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33676037
>>>A) Delete the database record for a file AND
B) Delete the physical file

?

Yes. I need to delete both files.

>>>>Also, it is supposed to operate on multiple files at one time?

No. I want to limit CEP to using one thumbnail image per document. I have told them: One thumbnail image per document only! =)

So far I am not able to Delete either database record or file, using the code below. I think I need to change these paths:

      <cfif FileExists("c:\upload\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>

      <cffile action="delete" file="c:\upload\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">

... because I have not defined #cffile.serverFile# in either of those paths.

I already have a statement:  <cfset uploadedImage  = #cffile.serverFile#>

And I am working with that. More in a while. Thank you again,

Eric
<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

            <cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">      

                   

      <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

          <!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="makeunique">

            <cfset uploadedImage  = #cffile.serverFile#>

                  

                  

                            <!--- list allowed file extensions and compare them to cffile.serverFileExt --->       

                  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

                            <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

                  </cfif>

                  

            <cfelse>

            

         <!--- SET uploadedImage to ""  --->       			

			<cfset uploadedImage = "">   

            

            

         <!--- if the Delete Document Thumbnail Image checkbox is checked (~ @ line 635-ish in code, below ~), then delete the document thumbnail image --->   

			<cfif isDefined('form.deleteDocumentImage')>

            



         <!--- test for existence of file  --->       

              <cfif FileExists("c:\upload\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>



             <cffile action="delete" file="c:\upload\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">

              <cfquery name="DeleteDocumentImage" datasource="#ds#">        

                  UPDATE tbl_CEP_Documents

				  SET DocumentImage = <cfqueryparam cfsqltype="cf_sql_varchar" value="null">,

				  WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">

			</cfquery>

                     

            </cfif>

	        <!--- END test for existence of file  --->       



            </cfif>

			<!--- END cfif isDefined('form.deleteDocumentImage')  --->



            </cfif>

            <!----- END form.DocumentImage neq "" ---->

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 33676246
>> I think I need to change these paths:

Yes, the CFFILE variables won't be involved at all for deletes.  That structure only exists after you upload a file.

What I usually do is pass the record ID in a form field. (IMO passing the file name instead is problematic and is also slightly less secure).

<input type="checkbox" name="deleteDocumentImage" value="#theRecordIDYouWantToDelete#" />

Then on the action page, I first run a query to get the file name.  Then delete the record.  Finally, I delete the physical file

<cfif structKeyExists(FORM, "deleteDocumentImage")>
      <cfquery name="findDocument" ....>
              SELECT  TheDocumentFileName FROM SomeTable
              WHERE  TheRecordID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
      </cfquery>

      <!--- If the record exists ....--->
      <cfif findDocument.recordCount>
              <cfquery name="deleteDocument" ....>
                    DELETE FROM SomeTable
              WHERE  TheRecordID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
             </cfquery>

             <cfset pathToFile = "c:\path\To\YourFiles\"& findDocument.deleteDocumentImage>

      </cfif>

</cfif>



If you need to delete both the db record AND the physical file, I would
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 357 total points
ID: 33676285
... Darn, that posted before I was done.  The full psuedo-code snippet for the DELETE action should be:

<cfif structKeyExists(FORM, "deleteDocumentImage")>

      <!--- First verify the record exists ....--->
      <cfquery name="findDocument" ....>
              SELECT  TheDocumentFileName FROM SomeTable
              WHERE  TheRecordID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
      </cfquery>

      <!--- If it does exist ....--->
      <cfif findDocument.recordCount>
              <!---- If the db record exists, delete it .... --->
              <cfquery name="deleteDocument" ....>
                    DELETE FROM SomeTable
              WHERE  TheRecordID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
             </cfquery>

              <!---- Only delete the physical file if it exists (ie avoid errors) .... --->
             <cfset pathToFile = "c:\path\To\YourFiles\"& findDocument.deleteDocumentImage>
             <cfif FileExists(pathToFile)>
                  <cffile action="delete" file="#pathToFile#">
             </cfif>
      </cfif>
</cfif>



0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33677679
This makes a lot of sense. I implemented your code and get this error:

Invalid data PrelimProgram draft_Page_03_Image_0003.jpg for CFSQLTYPE CF_SQL_INTEGER.   The error occurred in C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 81
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 75
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 53
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 39
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 1


76: <cfif structKeyExists(FORM, "deleteDocumentImage")>
77: <!--- query to see that DocumentImage file is checked to be deleted --->

78: <cfquery name="getDocumentImageToDelete" datasource="#ds#">
79 : SELECT DocumentImage
80 : FROM tbl_CEP_Documents
81 : WHERE DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
82 : </cfquery>
Hmm. So ColdFusion thinks that the DocumentImage file name --  PrelimProgram draft_Page_03_Image_0003.jpg -- is the DocumentID.
But in query getDocumentImageToDelete, the WHERE clause does request a DocumentID -- not the file name.
What am I missing here?
I really appreciate your patience and time.

Eric

<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

            <cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">      

                   

      <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

          <!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="makeunique">

            <cfset uploadedImage  = #cffile.serverFile#>

                  

                  

                            <!--- list allowed file extensions and compare them to cffile.serverFileExt --->       

                  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

                            <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

                  </cfif>

                  

            <cfelse>

            

         <!--- SET uploadedImage to ""  --->       			

			<cfset uploadedImage = "">   

            

            

<cfif structKeyExists(FORM, "deleteDocumentImage")>



<!--- query to see that DocumentImage file is checked to be deleted --->

<cfquery name="getDocumentImageToDelete" datasource="#ds#">

    SELECT DocumentImage

    FROM tbl_CEP_Documents

    WHERE DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">

</cfquery>





<!--- if DocumentImage file is checked to be deleted, then do this: --->



      <cfif findDocument.recordCount>

              <!---- If the db record exists, delete it .... --->

              <cfquery name="deleteDocumentImage" datasource="#ds#">

              DELETE FROM tbl_CEP_Documents

              WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">

             </cfquery>



              <!---- Only delete the physical file if it exists (i.e. avoid errors) .... --->

             <cfset pathToFile = "c:\upload\cep-dc.org\&findDocument.deleteDocumentImage">

             <cfif FileExists(pathToFile)>

                  <cffile action="delete" file="#pathToFile#">

             </cfif>

      </cfif>

</cfif>





            </cfif>

            <!----- END form.DocumentImage neq "" ---->

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 33677934
It sounds like you didn't change the form field value.  Originally it was the file name, but it needs to be the numeric record ID instead.

<input type="checkbox" name="deleteDocumentImage" value="#theRecordIDYouWantToDelete#" />
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33677963
>>  <cfset pathToFile = "c:\upload\cep-dc.org\&findDocument.deleteDocumentImage">


Also, make sure to use the file name ("DocumentImage" I think ?)

<cfset pathToFile = "c:\upload\cep-dc.org\"&  findDocument.DocumentImage>



0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33678119
Making progress.

In the form field -- the Mark For Deletion checkbox -- I changed the value from

value="#getDocumentImage.DocumentID#"

to

value="#val(form.DocumentID)#"

Thus:

 <!--- Mark For Deletion checkbox --->      
 <input type="checkbox" name="deleteDocumentImage" value="#val(form.DocumentID)#" class="border0" />

And that got rid of the "Invalid data PrelimProgram draft_Page_03_Image_0003.jpg for CFSQLTYPE CF_SQL_INTEGER." error.

Now I get an error: Element RECORDCOUNT is undefined in FINDDOCUMENT.

85 : <!--- if DocumentImage file is checked to be deleted, then do this: --->
86 :
87 :       <cfif findDocument.recordCount>
88 :               <!---- If the db record exists, delete it .... --->
89 :               <cfquery name="deleteDocumentImage" datasource="#ds#">

If the query [to see that DocumentImage file is checked to be deleted] is named getDocumentImageToDelete, should I do:

recordCount.getDocumentImageToDelete

... (I think so).

However, now I am concerned about something. Won't this query:

     <cfquery name="deleteDocumentImage" datasource="#ds#">
              DELETE FROM tbl_CEP_Documents
              WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
             </cfquery>

delete the entire record?

I want to delete only the value in column DocumentImage. So should I do:

     <cfquery name="deleteDocumentImage" datasource="#ds#">
              DELETE DocumentImage
              FROM tbl_CEP_Documents
              WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
             </cfquery>

?
<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->

            <cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">      

                   

      <!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->       

          <!--- this is a document thumbnail image --->       

            <cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="makeunique">

            <cfset uploadedImage  = #cffile.serverFile#>

                  

                  

                            <!--- list allowed file extensions and compare them to cffile.serverFileExt --->       

                  <cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>

                            <cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">

                  </cfif>

                  

            <cfelse>

            

         <!--- SET uploadedImage to ""  --->       			

			<cfset uploadedImage = "">   

            

            

<cfif structKeyExists(FORM, "deleteDocumentImage")>



<!--- query to see that DocumentImage file is checked to be deleted --->

<cfquery name="getDocumentImageToDelete" datasource="#ds#">

    SELECT DocumentImage

    FROM tbl_CEP_Documents

    WHERE DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">

</cfquery>





<!--- if DocumentImage file is checked to be deleted, then do this: --->



      <cfif findDocument.recordCount>

              <!---- If the db record exists, delete it .... --->

              <cfquery name="deleteDocumentImage" datasource="#ds#">

              DELETE FROM tbl_CEP_Documents

              WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">

             </cfquery>



              <!---- Only delete the physical file if it exists (i.e. avoid errors) .... --->

             <cfset pathToFile = "c:\upload\cep-dc.org\&findDocument.DocumentImage">

             <cfif FileExists(pathToFile)>

                  <cffile action="delete" file="#pathToFile#">

             </cfif>

      </cfif>

</cfif>





            </cfif>

            <!----- END form.DocumentImage neq "" ---->

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 33678614
>> If the query [to see that DocumentImage file is checked to be deleted] is named >>getDocumentImageToDelete, should I do:

Oops. I didn't notice the query name didn't match.  Yeah, change it to match your query name

ie <cfif getDocumentImageToDelete.recordCount>
         .....
    </cfif>


>> However, now I am concerned about something. Won't this query:
>> delete the entire record?

Ohh... yes, it will.


>> I want to delete only the value in column DocumentImage.

Then what you want is to UPDATE the column.  Just set the DocumentImage to NULL. I think that should produce the results you're looking for

              UPDATE tbl_CEP_Documents
              SET       DocumentImage
              WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">

0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 357 total points
ID: 33678616
Correction:

UPDATE tbl_CEP_Documents
SET       DocumentImage = NULL
WHERE  DocumentID = <cfqueryparam value="#form.deleteDocumentImage#" cfsqltype="cf_sql_integer">
0
 
LVL 3

Author Comment

by:Eric Bourland
ID: 33679756
I think it's working. =)

I wil test further tomorrow.

Hope you are well. Good night.

Eric
0
 
LVL 3

Author Closing Comment

by:Eric Bourland
ID: 33686875
This is working like a charm. Thank you to _agx_,  myselfrandhawa, SidFishes, and mattibutt. As usual I have learned a great deal from this task.

I really appreciate your time and patience. And expertise.

Eric B
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33689698
Glad everything is working smoothly now.  

(Sorry I've been rather silent. What a week ... and it's still going ;-)

Peace
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now