Downloads a file from the server to the client. Sets the file or MIME content encoding of content returned by the current page. Sets the name of a file that is downloaded by the current page.
Note: For this tag execute, it must be enabled in the ColdFusion Administrator. For more information, see Administering ColdFusion MX.
<cfcontent type = "file_type" deleteFile = "Yes" or "No" file = "filename" reset = "Yes" or "No">
Note: If you call this tag from a custom tag, and you do not want the tag to discard the current page when it is called from another application or custom tag, set reset
= "no".
To set the character encoding of generated output, use code such as the following:
<cfcontent type="text/html; charset=ISO-8859-1">
If a file delete operation is unsuccessful, ColdFusion throws an error.
When ColdFusion processes an HTTP request, it determines the character set of the data returned in the HTTP response. By default, ColdFusion returns character data using the Unicode UTF-8 format (regardless of the value of an HTML meta
tag in the page). Within a ColdFusion page, you can override the default character encoding of the response, using the cfcontent
tag. To specify the MIME type and character set of the page output, use the type
attribute, as follows:
<cfcontent type="text/html charset=EUC-JP">
The following example shows how you might display the results of a query, in an Excel spreadsheet within a ColdFusion page.
<!--- use cfsetting to block output of HTML that is not within cfoutput tags --->
<cfsetting enablecfoutputonly="Yes"> <!--- get Employee info ---> <cfquery name="GetEmps" datasource="CompanyInfo"> SELECT * FROM Employee </cfquery> <!--- Set variables for special chars ---> <cfset TabChar = Chr(9)> <cfset NewLine = Chr(13) & Chr(10)> <!--- Set content type to invoke Excel ---> <cfcontent type="application/msexcel"> <!--- suggest default name for XLS file ---> <!--- use "Content-Disposition" in cfheader for Internet Explorer ---> <cfheader name="Content-Disposition" value="filename=Employees.xls"> <!--- output data, each row on one line---> <cfloop query="GetEmps"> <cfoutput>#Emp_ID##TabChar##LastName# #TabChar##FirstName##TabChar##Salary##NewLine#</cfoutput> </cfloop>
If you use this tag after the cfflush
tag on a page, ColdFusion throws an error.
<!--- This example shows the use of cfcontent to return the contents of the CF Documentation page dynamically to the browser. You might need to change the path and/or drive letter. (graphics do not display) ---> <!--- Files may be set to delete after downloading, allowing for posting of changing content. ---> <cfcontent type = "text/html" file = "c:\inetpub\wwwroot\cfdocs\main.htm" deleteFile = "No"> <!--- This example shows how reset attribute changes text output. ---> <html> <body> <h3>cfcontent Example 2</h3> <p>This example shows how reset attribute changes output for text.</p> <p>reset = "Yes ": 123 <cfcontent type = "text/html" reset = "Yes ">456</p> <p>This example shows how reset attribute changes output for text.</p> <p>reset = "No ": 123 <cfcontent type = "text/html" reset = "No ">456</p>