Solved

my <cfcontent code is wrong

Posted on 2014-04-07
20
235 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:myselfrandhawa
Comment Utility
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:myselfrandhawa
Comment Utility
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
Comment Utility
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:myselfrandhawa
Comment Utility
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
Comment Utility
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:myselfrandhawa
Comment Utility
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
Comment Utility
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
Comment Utility
oh, this is the Html source code.
SOURCE.htm
0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
ok
0
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
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
Comment Utility
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
Comment Utility
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:myselfrandhawa
Comment Utility
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:myselfrandhawa
Comment Utility
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
Comment Utility
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
Comment Utility
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:
myselfrandhawa earned 500 total points
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now