cfcontent

Description

Does either or both of the following:

  • Sets the MIME content encoding header for the current page; if the encoding information includes a character encoding, sets the character encoding of generated output.

  • Sends the contents of a file, or of a variable that contains binary data, as the page output.

To restrict this tag, use the settings in the ColdFusion Administrator > Security > Sandbox Security. For more information, see the Administrator online Help.

Syntax

<cfcontent  
    deleteFile = "yes|no" 
    file = "filename" 
    reset = "yes|no" 
    type = "file type" 
    variable = "variable name">
Note: You can specify this tag’s attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag’s attribute names as structure keys.

History

ColdFusion 8: Changed the behavior of the tag if the type attribute is not specified and the file attribute is specified. Previously, ColdFusion assumed a default file type of text/html. Now, ColdFusion attempts to get the content type from the file.

ColdFusion MX 7: Added the variable attribute.

Attributes

Attribute

Req/Opt

Default

Description

deleteFile

Optional

no

Applies only if you specify a file with the file attribute.

  • yes: deletes the file on the server after sending its contents to the client.

  • no: leaves the file on the server.

file

Optional

Name of an on-disk or in-memory file whose contents provide the page output. The filename must start with a drive letter and a colon, or a forward or backward slash. When using ColdFusion in a distributed configuration, the file attribute must refer to a path on the system on which the web server runs. When you use this attribute, any other output on the current CFML page is ignored; only the contents of the file are sent to the client.

reset

Optional

yes

If you specify a file or variable attribute, this attribute has no effect; otherwise, it does the following:

  • yes: discards output that precedes call to cfcontent

  • no: preserves output that precedes call to cfcontent. In this case, all output is sent with the specified type.

type

Optional

The MIME content type of the page, optionally followed by a semicolon and the character encoding. By default, ColdFusion sends pages as text/html content type in the UTF-8 character encoding. However, if the file attribute is specified, ColdFusion attempts to get the content type from the file.

The content type determines how the browser or client interprets the page contents.

The following are some of the content type values that you can use:

  • text/html

  • text/plain

  • application/x-shockwave-flash

  • application/msword

  • image/jpeg

The following list includes commonly used character encoding values:

  • utf-8

  • iso-8859-1

  • windows-1252

  • us-ascii

  • shift_jis

  • iso-2022-jp

  • euc-jp

  • euc-kr

  • big5

  • euc-cn

  • utf-16

For example:

type = "text/html" 
type = "text/html; charset=ISO-8859-1"

variable

Optional

Name of a ColdFusion binary variable whose contents can be displayed by the browser, such as the contents of a chart generated by the cfchart tag or a PDF or Excel file retrieved by a cffile action="readBinary" tag. When you use this attribute, any other output on the current CFML page is ignored; only the contents of the file are sent to the client.

Usage

To set the character encoding (character set) of generated output, including the page HTML, use code such as the following:

<cfcontent type="text/html; charset=ISO-8859-1">

When ColdFusion processes an HTTP request, it determines the character encoding to use for the data it returns 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. You can use the cfcontent tag to override the default character encoding of the response. For example, to tell ColdFusion to return the page using Japanese EUC character encoding, use the type attribute, as follows:

<cfcontent type="text/html; charset=EUC-JP"> 

If you call the cfcontent 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".

If a file delete operation is unsuccessful, ColdFusion throws an error.

Do not use this tag after the cfflush tag on a page, it has no effect or ColdFusion throws an error.

The following tag can force most browsers to display a dialog box that asks users whether they want to save the contents of the file specified by the cfcontent tag using the filename specified by the filename value. If the user selects to open the file, most browsers open the file in the related application, not the browser window.

<cfheader name="Content-Disposition" value="attachment; filename=filename.ext">

Some file types, such as PDF documents, do not use executable code and can display directly in most browsers. To request the browser to display the file directly, use a cfheader tag similar to the following:

<cfheader name="Content-Disposition" value="inline; filename=name.ext">

You can use any value for the filename part of the filename attribute, but the ext part must be the standard Windows extension for the file type.

For file types that might contain executable code, such as Microsoft Excel documents, most browsers always ask before opening the document. For these file types, the inline content disposition specification requests the browser to display the file directly if the user selects to open the file.

For more information on character encodings, see the following web pages:

For a complete list of media types used on the Internet, see www.iana.org/assignments/media-types/.

Example

<!--- CFCONTENT Example 1  
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 depending on how ColdFusion is installed on your 
system. Notice that the graphics do not display and the hyperlinks do not work, 
because the html page uses relative filename references. 
The root of the reference is the ColdFusion page, not the location of the 
html page. ---> 
 
<cfcontent type = "text/html"  
    file = "C:\ColdFusion9\wwwroot\cfdocs\dochome.htm"  
    deleteFile = "no"> 
 
<!--- CFCONTENT Example 2  
This example shows how the Reset attribute changes text output. Notice how the 
first text section ("This example shows how the Reset attribute changes output 
for text reset = "Yes":123) does NOT print out to the screen. ---> 
 
<p>This example shows how the Reset attribute changes output for text.</p> 
<p>reset = "Yes": 123 <BR> <cfcontent type = "text/html" reset = "Yes">456</p> 
<p>This example shows how the Reset attribute changes output for text.</p> 
<p>reset = "No": 123 <BR> <cfcontent type = "text/html" reset = "No">456</p> 
<!--- CFCONTENT Example 3  
This example triggers a download of an Excel file. The user is prompted with an option to save the file or open it in the browser. ---> 
 
<cfheader name="Content-Disposition" value="inline; filename=acmesales03.xls">  
    <cfcontent type="application/vnd.ms-excel" file="c:\temp\acmesales03.xls"> 
 
<!--- CFCONTENT Example 4 
This example triggers a download of a Word document then deletes the original from the "temp" directory. The user is prompted with an option to save the file or open it in the browser. ---> 
 
<cfheader name="Content-Disposition" value="inline; filename=temp.doc"> 
<cfcontent type="application/msword" file="c:\temp\Cable.doc" deletefile="yes">  
 
<!--- CFCONTENT Example 5  
This example causes the browser to treat the HTML table as Excel data.  
Excel interprets the table format. 
Because Excel can include executable code, the browser prompts the user whether 
to save the file or open it in a browser. ---> 
 
<cfheader name="Content-Disposition" value="inline; filename=acmesalesQ1.xls"> 
<cfcontent type="application/vnd.msexcel"> 
 
<table border="2"> 
<tr><td>Month</td><td>Quantity</td><td>$ Sales</td></tr> 
<tr><td>January</td><td>80</td><td >$245</td></tr> 
<tr><td>February</td><td>100</td><td>$699</td></tr> 
<tr><td>March</td><td>230</td><td >$2036</td></tr> 
<tr><td>Total</td><td>=Sum(B2..B4)</td><td>=Sum(C2..C4)</td></tr> 
</table>