Solved

my <cfcontent code is wrong

Posted on 2014-04-07
20
245 Views
Last Modified: 2014-04-09
Hi,

when I run my page. I get a binary data.

Something wrong with my cfcontent.  I have a file stored in the database as an object file.

I need to be able to download this file and save it.

thank you,
lulu

 <CFIF isDefined("QGetFile") and #QGetFile.RecordCount# gt 0>
						<cfoutput query="QGetFile">
                        #QGetFile.FileName#
                        
                     
                       <cfheader
      name="content-disposition"
      value="inline; filename=#QGetFile.FileName#"
      />
      
      <cfheader name="Content-Disposition" value="inline; filename=#QGetFile.FileName#">  
  
<cfcontent type="application/msword" variable="#toBinary(QGetFile.UploadedFile)#"> 

                     
                        
                        </cfoutput> 
                      </CFIF>

Open in new window

0
Comment
Question by:lulu50
  • 10
  • 10
20 Comments
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39983897
Try this Code

File name is cgi.cfm, so i am calling the same file in the window.location.href tag, please note, i am passing id too so page should not throw error


<script>
function openWindow(filename,id) {
	window.location.href = "cgi.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name="QGetFile">
    SELECT name 
        FROMmytable
        WHERE ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ID#">
</cfquery>
<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
  	<a href="javascript:void(0);" onclick="openWindow('#QGetFile.name#','#URL.ID#')">#QGetFile.name#</a> 
  </cfoutput>
  <div class="downloadfile"></div>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<cfcontent type="image/jpeg" deletefile="no" file = "#ExpandPath('#url.file#')#">
</cfif>

Open in new window

0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39983923
Change the mimetype what you need here, i see you added as application/msword

Mine code is having : image/jpeg, so change that according to your needs.
0
 

Author Comment

by:lulu50
ID: 39983970
myselfrandhawa,

Thank you for looking into this!!!!

I have an error:  

ByteArray objects cannot be converted to strings.

this is what I changed:

  <script>
function openWindow(filename,id) {
	window.location.href = "cgi.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile"  datasource='#strDSN#' username='#strUID#' password='#strPWD#'>
			select UploadedFile from files
            where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
		</cfquery>     
<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
  	<a href="javascript:void(0);" onclick="openWindow('#QGetFile.UploadedFile#','#URL.ID#')">#QGetFile.UploadedFile#</a> 
  </cfoutput>
  <div class="downloadfile"></div>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#">
</cfif>     

Open in new window

0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39983990
1. Can you show me the dump of the query, what actually your query is returning,

2. Check the Path which has been specified in the cfcontent tag
<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#">

Open in new window


It should actually get the file from that path, if it is under some folder try using

#ExpandPath('somefoldername/#url.file#')#

Open in new window


PS, I would see the query results, to debug more, as i ran the same code on my local and it worked fine

One more things, Is the filename [CGI.CFM] is the same as what is your filename, keep that same.

I mean if you filename is whatever.cfm, change the code in the script tag to whatever.cfm to it should point to the same file and i hope you are not passing any extra attributes, with respect to the ID only,
0
 

Author Comment

by:lulu50
ID: 39984017
binary
the name of my file is ViewLogEntry.cfm

so, I changed the code.

               
   
  <script>
function openWindow(filename,id) {
	window.location.href = "ViewLogEntry.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
			select UploadedFile from files
            where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
		</cfquery>    
        
     <cfdump var="#QGetFile#" /> 
     <cfdump var="#ExpandPath('#url.file#')#" /> 
<cfoutput>SQL: #r.SQL#</cfoutput> 


<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
  	<a href="javascript:void(0);" onclick="openWindow('#QGetFile.UploadedFile#','#URL.ID#')">#QGetFile.UploadedFile#</a> 
  </cfoutput>
  <div class="downloadfile"></div>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#">
</cfif>                

Open in new window

0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39984044
in which format, you are storing it,

you can use the following to download the file if it is stored in base64 format

writeOutput(toString(getBLOB.file));

Open in new window


like this

  <script>
function openWindow(filename,id) {
	window.location.href = "ViewLogEntry.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
    select UploadedFile from files
    where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
</cfquery>    
        
 <cfdump var="#QGetFile#" /> 
 <cfset convertfile = toString(QGetFile.UploadedFile)>
 <cfdump var="#ExpandPath('#url.file#')#" /> 
<cfoutput>SQL: #r.SQL#</cfoutput> 


<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
    <cfset convertfile = toString(QGetFile.UploadedFile)> 
  	<a href="javascript:void(0);" onclick="openWindow('#convertfile#','#URL.ID#')">#convertfile#</a> 
  </cfoutput>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#">
</cfif> 

Open in new window


 try to use this in the cfcontent tag

Can you show the source code of the page, generated source code, by running my code i provided here, changed one
0
 

Author Comment

by:lulu50
ID: 39984077
myselfrandhawa,

I don't have any error but this is how my page look like.

I want it to be a link when the user click on it will have the option to either save or open.

I don't know where to put this " writeOutput(toString(getBLOB.file));


BINARY


                 
   
  <script>
function openWindow(filename,id) {
	window.location.href = "ViewLogEntry.cfm?filter-1&ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
    select UploadedFile from files
    where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
</cfquery>    
        
 <cfdump var="#QGetFile#" /> 
 <cfset convertfile = toString(QGetFile.UploadedFile)>
<cfoutput>SQL: #r.SQL#</cfoutput> 


<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
    <cfset convertfile = toString(QGetFile.UploadedFile)> 
  	<a href="javascript:void(0);" onclick="openWindow('#convertfile#','#URL.ID#')">#convertfile#</a> 
  </cfoutput>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#">
    
</cfif>     

Open in new window

       



THIS IS MY INSERT

<!--- Insert Uploaded files --->
 <cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
             
             <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
             <cfset variables["FileName" & ii] = Form["FileName" & 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>

            <cfif len(trim(variables.Attachments))> 
   
      <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
			Set Nocount On;
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	
      <cfif FileExists(variables.filelocation & ii)>
      
                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        ( 
                        <cfif #variables["Descp" & cnt]# neq ''>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">, 
                        <cfelse>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">, 
                        </cfif>
                     
                        <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(variables.filelocation & ii)#">,
                    #QValidateIssueID.IssueID#, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" />  )          
        </cfif> 
      </cfloop>
</cfquery>
</cfif>

Open in new window



DATA
0
 

Author Comment

by:lulu50
ID: 39984086
oh, this is the Html source code.
SOURCE.htm
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39984091
Ok, First it is not good to store the value as binary format as you did in your code, you can easily store the file name and then fetch that file from the path and download it easily,

Let me replicate this again and i will get back to you
0
 

Author Comment

by:lulu50
ID: 39984095
myselfrandhawa ,

no wait please,  I have to store the file as binary for security issue here at the company.

otherwise, I would have saved the file to a folder.

I don't have a permission to write to any file on the server.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39984130
ok
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39984196
The this line should work fine
<cfcontent variable="#url.file#" reset="true" type="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

There are few things:

1. You are storing the file as path like c:\inetpub\wwwroot\project1\uploads\myfile.jpg

2. The data is being truncated while being displayed on the page..

Try doing the following..

1. Do not add the path to the database, just add the filename as the readasbinary and then read it through the exapnd path in the cfcontent tag specifying the path and then the filename

that will get you going

if still not clear, let me know
0
 

Author Comment

by:lulu50
ID: 39984530
can you please help me with this:

"Do not add the path to the database, just add the filename as the readasbinary and then read it through the exapnd path in the cfcontent tag specifying the path and then the filename "

I don't know how to do this.

I don't know how to just store the name of the file instead of the full path.


<!--- Insert Uploaded files --->
 <cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
             
             <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
             <cfset variables["FileName" & ii] = Form["FileName" & 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>

            <cfif len(trim(variables.Attachments))> 
   
      <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
			Set Nocount On;
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	
      <cfif FileExists(variables.filelocation & ii)>
      
                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        ( 
                        <cfif #variables["Descp" & cnt]# neq ''>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">, 
                        <cfelse>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">, 
                        </cfif>
                     
                        <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(variables.filelocation & ii)#">,
                    #QSubLogEntry.IssueID#, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" />  )          
        </cfif> 
      </cfloop>
</cfquery>
</cfif>

Open in new window

0
 

Author Comment

by:lulu50
ID: 39984551
binary

   <script>
function openWindow(filename,id) {
	window.location.href = "ViewLogEntry.cfm?filter-1&ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
    select UploadedFile from files
    where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
</cfquery>    
        
 <!---<cfdump var="#QGetFile#" />  --->
 <cfset convertfile = toString(QGetFile.UploadedFile)>
<!---<cfoutput>SQL: #r.SQL#</cfoutput>  --->


<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile"> 
    <cfset convertfile = toString(QGetFile.UploadedFile)> 
  	<a href="javascript:void(0);" onclick="openWindow('#convertfile#','#URL.ID#')">#convertfile#</a> 
  </cfoutput>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
	<cfcontent type="application/x-unknown">
	<cfheader name="Content-disposition" value="attachment;filename=#url.file#">
	<!---<cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('#url.file#')#"> --->
    
    <cfcontent variable="#url.file#" reset="true" type="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
    
    
</cfif>             

Open in new window

0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39985057
just change this

<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(variables.filelocation & ii)#">,
 

to

<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(ii)#">,

You variables.filelocation is static, somehwre you might be storing the files...
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39985426
instead of this

<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(ii)#">,

use this

<cfqueryparam cfsqltype="cf_sql_blob"
                                value="#tobase64(ii)#">,

and when you server the file with cfcontent tag

use this

<cfcontent variable="#ExpandPath('uploadedfiles/#ToSring(ToBinary(url.file))#')#" reset="true" type="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
0
 

Author Comment

by:lulu50
ID: 39985757
I have this error when I changed to this line:

 <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(ii)#">,





error


<!--- Insert Uploaded files --->
 <cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
             
             <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
             <cfset variables["FileName" & ii] = Form["FileName" & 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>

            <cfif len(trim(variables.Attachments))> 
   
      <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
			Set Nocount On;
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	
      <cfif FileExists(variables.filelocation & ii)>

                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        ( 
                        <cfif #variables["Descp" & cnt]# neq ''>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">, 
                        <cfelse>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">, 
                        </cfif>
                     
                        <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(ii)#">,
                    #IssueIDVal#, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" />  )          
        </cfif> 
      </cfloop>
</cfquery>
</cfif>

Open in new window

0
 

Author Comment

by:lulu50
ID: 39985774
when I change it to this line:
   <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#tobase64(ii)#">,

I get this error:

error
0
 
LVL 15

Accepted Solution

by:
Gurpreet Singh Randhawa earned 500 total points
ID: 39987040
Check this code, i had done on my local system:

Few changes i made..,

1. I am using longtext field instead of blog field in the database, I ned to find a good reason to use blog field, as of now for security purpose, i am just using the tobase64 to convert the image name to be safe and get it stored in the database

Here is the code portion:

Database:

CREATE TABLE IF NOT EXISTS `files` (
  `fileID` int(11) NOT NULL AUTO_INCREMENT,
  `issueID` int(11) DEFAULT NULL,
  `filename` varchar(255) DEFAULT NULL,
  `uploadedfile` longtext,
  `status` int(11) NOT NULL,
  `createddate` datetime NOT NULL,
  PRIMARY KEY (`fileID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `files`
--

INSERT INTO `files` (`fileID`, `issueID`, `filename`, `uploadedfile`, `status`, `createddate`) VALUES
(4, 1, 'C:\\ColdFusion10\\cfusion\\runtime\\work\\Catalina\\localhost\\tmp\\neotmp3863304578800732703.tmp', 'RGVzZXJ0LmpwZw==', 1, '2014-04-09 00:00:00'),
(5, 1, 'C:\\ColdFusion10\\cfusion\\runtime\\work\\Catalina\\localhost\\tmp\\neotmp965781705400032480.tmp', 'bGFtYm9yZ2hpbmktODJhLmpwZw==', 1, '2014-04-09 00:00:00');


<!--- Insert Uploaded files --->
<cfsetting showdebugoutput="yes">
<cfset filelocation = expandpath('uploadedfiles/')>
<cfset maxFiles = 3>
<cfset Attachments="">
<cfif isdefined('form.submit')>
<cfloop from="1" to="#variables.maxFiles#" index="ii">
  <cftry>
    <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
      <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
      <cfset variables["FileName" & ii] = Form["AttachFile" & ii]>
      <CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
      <cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>
    </cfif>
    <cfcatch>
    <cfoutput>
      <br>
      #cfcatch.message#<br>
      #cfcatch.detail#<br>
      #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
    </cfoutput>  
    </cfcatch>
  </cftry>
</cfloop>
<cfif len(trim(variables.Attachments))>
  
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	
      <cfif FileExists(variables.filelocation & ii)>
      <cfquery name = "QLogFiles">
                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        ( 
                        <cfif #variables["Descp" & cnt]# neq ''>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">, 
                        <cfelse>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">, 
                        </cfif>
                     
                        <cfqueryparam cfsqltype="cf_sql_longvarchar"
                                value="#ToBase64(ii)#">,
                    1, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" /> );
        
        </cfquery>
        </cfif>
      </cfloop>
</cfif>
</cfif>
<form method="post" action="index.cfm" enctype="multipart/form-data">
<cfoutput>
<cfloop from="1" to="3" index="ii">
	<INPUT TYPE="text" NAME="FileDescription#ii#" id="FileDescription#ii#" style="width:400px;">
	<INPUT TYPE="FILE" NAME="AttachFile#ii#" id="AttachFile#ii#" onchange="return validateFileExtension(this);"
		class="file" style="width:400px;">
	<input type="button" value="Delete" name="DeleteFile#ii#" id="DeleteFile#ii#"
		style="height:20px;float:right;margin-right:87px;" 
		onclick="clearFile('AttachFile' + #ii#,'FileDescription' + #ii# );" />
	<br /><br />
</cfloop>
</cfoutput>
 <input type="submit" name="submit" value="Submit">
</form>

<script>
function openWindow(filename,id) {
	window.location.href = "download.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile">
    select UploadedFile from files
    where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
</cfquery>

<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile">
    <cfset convertfile =  #tostring(tobinary(QGetFile.UploadedFile))#>
    <a href="javascript:void(0);" onclick="openWindow('#convertfile#','#URL.ID#')">#convertfile#</a> </cfoutput>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
  <cfcontent type="application/x-unknown">
  <cfheader name="Content-disposition" value="attachment;filename=#url.file#">
  <cfcontent type="application/msword" deletefile="no" file = "#ExpandPath('uploadedfiles\#url.file#')#">
</cfif>

Open in new window


The above works fine and it should get you going by downloading the file..

Your way of working now:

<!--- Insert Uploaded files --->
<cfsetting showdebugoutput="yes">
<cfset filelocation = expandpath('uploadedfiles/')>
<cfset maxFiles = 3>
<cfset Attachments="">
<cfif isdefined('form.submit')>
<cfloop from="1" to="#variables.maxFiles#" index="ii">
  <cftry>
    <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
      <cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
      <cfset variables["FileName" & ii] = Form["AttachFile" & ii]>
      <CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
      <cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>
    </cfif>
    <cfcatch>
    <cfoutput>
      <br>
      #cfcatch.message#<br>
      #cfcatch.detail#<br>
      #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
    </cfoutput>  
    </cfcatch>
  </cftry>
</cfloop>
<cfif len(trim(variables.Attachments))>
  
				<cfset cnt=0>
                   <cfloop list="#variables.Attachments#" index="ii">
            <cfset cnt++/>	
      <cfif FileExists(variables.filelocation & ii)>
      <cfquery name = "QLogFiles">
                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        ( 
                        <cfif #variables["Descp" & cnt]# neq ''>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">, 
                        <cfelse>
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["FileName" & cnt]#">, 
                        </cfif>
                     
                        <cfqueryparam cfsqltype="cf_sql_longvarchar"
                                value="#ToBase64(variables.filelocation & ii)#">,
                    1, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" /> );
        
        </cfquery>
        </cfif>
      </cfloop>
</cfif>
</cfif>
<form method="post" action="index.cfm" enctype="multipart/form-data">
<cfoutput>
<cfloop from="1" to="3" index="ii">
	<INPUT TYPE="text" NAME="FileDescription#ii#" id="FileDescription#ii#" style="width:400px;">
	<INPUT TYPE="FILE" NAME="AttachFile#ii#" id="AttachFile#ii#" onchange="return validateFileExtension(this);"
		class="file" style="width:400px;">
	<input type="button" value="Delete" name="DeleteFile#ii#" id="DeleteFile#ii#"
		style="height:20px;float:right;margin-right:87px;" 
		onclick="clearFile('AttachFile' + #ii#,'FileDescription' + #ii# );" />
	<br /><br />
</cfloop>
</cfoutput>
 <input type="submit" name="submit" value="Submit">
</form>


<script>
function openWindow(filename,id) {
	window.location.href = "download.cfm?ID=" + id + "&file=" + filename;
}
</script>
<cfquery name = "QGetFile">
    select UploadedFile from files
    where Status = 1 and IssueID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ID#">
</cfquery>

<cfsetting enablecfoutputonly="yes">
<CFIF isDefined("QGetFile") and QGetFile.RecordCount gt 0>
  <cfoutput query="QGetFile">
    <cfset convertfile =  #tostring(tobinary(QGetFile.UploadedFile))#>
    <a href="javascript:void(0);" onclick="openWindow('#jsstringformat(convertfile)#','#URL.ID#')">#ListLast(convertfile,'\')#</a> </cfoutput>
</CFIF>
<cfif isDefined('url.file') and url.file neq ''>
  <cfcontent type="application/x-unknown">
  <cfheader name="Content-disposition" value="attachment;filename=#url.file#">
  <cfcontent type="application/msword" deletefile="no" file = "#url.file#">
</cfif>

Open in new window


Both the above codes are different, one is my way top one and second is your way, bottom one..

The only similarity is I am using Base64 to store in the file in the database and converting it again on the front end where the download.cfm is:

I am not using blog datatype, i am using long, so keep that in mind and you are good to go..
0
 

Author Closing Comment

by:lulu50
ID: 39988944
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
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, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

18 Experts available now in Live!

Get 1:1 Help Now