Solved

Update my file

Posted on 2014-04-11
4
173 Views
Last Modified: 2014-04-16
Hi,

I am not sure what I'm doing wrong to prevent it from updating my files.

I just want to update my files.  

I am doing something wrong but not sure what it is.

Thanks,
Lulu


<!--- begin update Uploaded files --->
<cfsetting showdebugoutput="yes">
 <cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
             <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
             <cfset variables["FileName" & ii] = Form["FileName" & ii]>
			 <cfset variables["FileID" & ii] = Form["FileID" & ii]>

            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>

            <CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
     
             <cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>  
          
        	</cfif>
            <cfcatch>
            <br>#cfcatch.message#<br>#cfcatch.detail#<br>
            #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
            </cfcatch></cftry>
      </cfloop>


      <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#" result="rs">
			Set Nocount On;
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	

               update Files 
               SET 

				<cfif #variables["Descp" & cnt]# neq ''>
					FileName = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">,
				<cfelse>
					FileName =  <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">,
				</cfif>
          
            <cfif len(trim(variables.Attachments))> 
          <cfif FileExists(variables.filelocation & ii)>
      			<cfif IsDefined('ToBase64(variables.filelocation & ii)') && #ToBase64(variables.filelocation & ii)# neq ''>
					UploadedFile = <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#ToBase64(variables.filelocation & ii)#">,
				</cfif>
                     
            	<cfif IsDefined('ToBase64(variables.filelocation & ii)') && #ToBase64(variables.filelocation & ii)# neq ''>
					uploadedasbinary = <cfqueryparam cfsqltype="cf_sql_blob" value="#FileReadBinary(variables.filelocation & ii)#">,
				</cfif>
         </cfif>
         </cfif>
   					CreatedDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#">

       		WHERE FileID = #variables["FileID" & ii]#  
      </cfloop>
</cfquery>


<cfoutput>SQL: #rs.SQL#</cfoutput> 

<!--- end of update file --->

Open in new window

0
Comment
Question by:lulu50
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39995022
Hi Lulu, I would like to see you exact code and what you need here is this like this:

1. Show the image or doc file on the screen.
2. upload another file and replace one.
3. delete existing files..

can you show your complete code here
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 250 total points
ID: 39995165
I hope this helps, I am not sure how to do the file conversion to base64 to upload into the database, haven't done that before.   I left that out just to show you an example of how this code can flow.

It is not necessary to use variables with counters on them inside your loop.   I have made the code simpler by using regular variables such as variable.fileID

             <cfset variables.Descp    = Form["FileDescription" & ii]>
             <cfset variables.FileName = Form["FileName" & ii]>

The other important change was that I put the CFQUERY inside your loop.   It's not a big deal to call your update three times and makes the code a lot easier if you just get your values together, update the database and then go to the next row and do the same.

Lastly, rather than using a CFIF statement to see if the value of the form field is empty I added NULL="" parameter of the cfqueryparam.   If the value of the variable is empty, this will insert a NULL into the database.   I wasn't sure where you were going with the CFIF statements, maybe I got it wrong??   Let me know, I think there is an easier way.


      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
             <cfset variables.Descp    = Form["FileDescription" & ii]>
             <cfset variables.FileName = Form["FileName" & ii]>
			 <cfset variables.FileID   = Form["FileID" & ii]>
			 <cfset Attachments = ""> <!---- assume there is no attachment --->

            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
	            <CFFILE ACTION="UPLOAD"
	                        FILEFIELD="AttachFile#ii#"
	                        DESTINATION="#variables.filelocation#"
	                        NAMECONFLICT="makeunique">
	             <cfset Attachments= cffile.serverfile>  
        	</cfif>
	
	        <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#" result="rs">
	               update Files 
	               SET 
					  fileDescription = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables.fileDescription#" null="#NOT len(variables.fileDescription)#">
					 ,FileName = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables.fileName#" null="#NOT len(variables.fileName)#">
	   				 ,CreatedDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#">
	       	       WHERE FileID = #variables.FileID#  
            </cfquery>

       <cfcatch>
       <br>#cfcatch.message#<br>#cfcatch.detail#<br>
       #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
       </cfcatch>
	   </cftry>
      </cfloop>

Open in new window

0
 
LVL 15

Assisted Solution

by:Gurpreet Singh Randhawa
Gurpreet Singh Randhawa earned 250 total points
ID: 39995664
Lulu, To show what you have added in the database as base64, to convert it back to the string

use the following:
toString(tobinary(filnema))

Open in new window


For other things, @gd is guiding you good
0
 

Author Closing Comment

by:lulu50
ID: 40003905
Thank you.

I got it to work!!!!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

12 Experts available now in Live!

Get 1:1 Help Now