Solved

Update my file

Posted on 2014-04-11
4
177 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 16

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 16

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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