cfcontent

Description

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.

Category

Data output tags

Syntax

<cfcontent 
  type = "file_type"
  deleteFile = "Yes" or "No"
  file = "filename"
  reset = "Yes" or "No"> 

See also

cfcol, cfoutput, cftable

Attributes

Attribute Req/Opt Default Description
type
Optional. You must specify one of these: type, file, reset
 
A file or MIME content type, optionally including character encoding, in which to return the page.
  • text/html
  • (any valid type)
The following character set values are typically used:
  • charset=UTF-8
  • charset=ISO-8859-1
  • charset=UTF-16
  • charset=US-ASCII
  • charset=UTF-16BE
  • charset=UTF-16LE
For example:
type = "text/html"
type = "text/html; charset = ISO-8859-1"
The semi-colon is optional.
For a list of character sets, see:
http://www.w3.org/International/O-charset-lang.html
deleteFile
Optional
No
Applies only if you specify a file with the file attribute.
  • Yes: deletes a file after the download operation
  • No
file
Optional
 
Name of file to get. When using ColdFusion in a distributed configuration, the file attribute must refer to a path on the system on which the web server runs.
reset
Optional
Yes
The reset and file attributes are mutually exclusive. If you specify a file, this attribute has no effect. See Note.
  • Yes: discards output that precedes call to cfcontent
  • No: preserves output that precedes call to cfcontent

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".

Usage

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.

Example

<!--- 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>

Comments