View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfcontent PreviousNext

cfcontent

Does either or both of the following:

Note: This tag executes only if it is enabled in the ColdFusion Administrator. For more information, see Configuring and Administering ColdFusion MX.

Data output tags

<cfcontent 
type = "file_type"
deleteFile = "Yes" or "No"
file = "filename"
reset = "Yes" or "No">starting with a drive letter and a colon, or a forward or backward slash

cfcol, cfheader, cfhttp, cfoutput, cftable

Attribute

Req/Opt

Default

Description

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.

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

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

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

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 file whose contents will be the page output. 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 is sent to the client.

reset

Optional

Yes

The reset and file attributes are mutually exclusive. If you specify a file, this attribute has no effect.

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

To set the character encoding (character set) of generated output, 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 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). You can use the cfcontent tag to override the default character encoding of the response. For example, to specify the character encoding of the page output as Japanese EUC, 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.

If you use this tag after the cfflush tag on a page, ColdFusion throws an error. The following tag can force most browsers to display a dialog that asks users whether they want to save the contents of the file specified by the cfcontent tag as a with the filename specified by the filename value.

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

For many file types, such as Excel documents, that Internet Explorer can display directly in the browser, the browser displays the file without asking users whether to save it if you use a cfheader tag similar to the following:

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

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

<!--- 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:\CFusionMX\wwwroot\cfdocs\dochome.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>

Contents > CFML Reference > ColdFusion Tags > cfcontent PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


sammyishere said on Dec 2, 2003 at 10:55 AM :
CFContent has some problems. I have scoured the forums and looked in many other places but I cannot render an excel or msword page into IE5 browser. I used to do it in cf5.0 but MX is still buggy. Then simple code below does not work!!!! Macromedia are you reading this??? If so you can reach me at sammyishere@Hotmail.com, my client is considering other application servers.!

<CFHEADER NAME="content-disposition" VALUE="inline; filename=book1.xls">
<cfcontent type="application/vnd.ms-excel" reset="no">

OR


<cfcontent type="application/vnd.ms-word" >

<cfheader name="Content-Disposition" value="attachment; filename=c:\cfusionmx\wwwroot\test.doc">
kuech said on Dec 31, 2003 at 11:20 AM :
The following should force the download of an Excel of Word document:

<cfheader name="Content-Type" value="unknown">
<cfheader name="Content-Disposition" value="attachment; filename=FileName.doc">
<cfcontent type="Application/Unknown" file="/http//downloads/FileName.doc" deletefile="No">
bougrinea said on Jan 23, 2004 at 12:35 PM :
I am using a CFCONTENT tag to try to open an Excel document. It works fine in any browser except Internet Explorer.

The code is:
<cfheader name="Content-Disposition" value="attachment;filename=mydownload.xls">
<cfcontent type="application/msexcel; charset=utf-8" file="#ExpandPath(".")#\document1.xls" deletefile="No">

When Netscape/Mozilla opens it, I get the popup dialog asking what I would like to do. However, when Internet Explorer opens it, I get output similar to the following:

ÐÏࡱá>þÿ þÿÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ó'ÍÑá°Áâ\


Any ideas as to how to get IE to behave would be greatly appreciated. I am running IE 6.0.2800.1106 with SP1.

Thanks.
lbryngel said on Feb 19, 2004 at 12:12 PM :
Proposed corrections:

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

The last part that mentions java should include the 1.4.2 specs as well.

CFMX 6.0 Updater 3 uses JDK 1.3 see this for encoding
java.sun.com/j2se/1.3/docs/guide/intl/encoding.doc.html

CFMX 6.1 uses JDK 1.4.2 see this for encoding support:
http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html

REMOVE NOTE:
"Note: This tag executes only if it is enabled in the ColdFusion Administrator. For more information, see Configuring and Administering ColdFusion MX"

The "Tag Restriction" page is not in CFMX 6.x Adminstrator - it was only in CF 5. To restrict this tag in CFMX use Sandbox Security.
lbryngel said on Feb 20, 2004 at 5:34 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace/add the current example on this page with the the following examples. If you have any comments on this example, add them to this page.

If these examples are added please maintain/add spaces between examples because code can only run on seperate CFM pages. Made code corrections/additions to first two examples and added 3 new.


<!--- CFCONTENT EXAMPLE 1
This example shows the use of cfcontent to return the contents of the
CFDocumentation 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 thehtml page.
--->
<cfcontent type = "text/html" file = "C:\Inetpub\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 --->

<html>
<body>
<h3>cfcontent Example 2</h3>
<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>
</body>
</html>


<!--- CFCONTENT EXAMPLE 3
This example triggers a download of an Excel file. User will be prompted
with an option to save or open file in 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 orginal
from the "temp" directory. User will be prompted with an option to save or
open file in 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 saves the HTML below the cfcontent tag as an Excel file. User will be
prompted to save or open the file in a browser. --->

<cfheader name="Content-Disposition" value="inline; filename=acmesalesQ1.xls">
<cfcontent type="application/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>
billsun said on Mar 2, 2004 at 1:46 PM :
I am using a CFCONTENT tag to try to gererate an Excel document which data come from SQL query. It works fine in Cold fusion 5.
But it doesn't work with CFMX + websphere (windows 2k)
my code look like:
------------------
<cfcontent type="application/vnd.ms-excel" reset="yes">

<table>
<cfoutput query="">
<tr>
<td>##</td>
</tr>
........
</cfoutput>
------------------------------------------------------

any idea
thanks in advance
my email : bill_sun@businteractive.com
DonaldBoers said on Mar 12, 2004 at 2:36 AM :
I use the download repeat region extension:

<!---Basic-UltraDev DownloadRepeatRegion--->
<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=schools.txt">
<CFCONTENT TYPE="application/txt">
Name,Principal,address 1,address 2,address 3,postal code,city,district,country
<cfoutput query="rsSchools">#rsSchools.schoolname#, #rsSchools.principal#, #rsSchools.address1#, #rsSchools.address2#, #rsSchools.address3#, #rsSchools.postalcode#, #rsSchools.city#, #rsSchools.district#, #rsSchools.country#</cfoutput>
<cfabort>

Everything works well, only the result is a long row with all the data.
I want (ofcourse every) record on a separate line.

Tom Muck in his explanation of the extension uses ASP and adds <%=vbcrlf%> before the </cfoutput>.
How can I translate this to Coldfusion?
Showfax TJ said on Mar 17, 2004 at 11:02 AM :
Try inserting a line break after the opening cfoutput tag

<cfoutput query="??">
line of text</cfoutput>

The cfoutput will copy exactly what's between the cfoutput tags, which above is [(linebreak)line of text]
raas1 said on Apr 27, 2004 at 1:32 PM :
Eu gostaria de saber se na tag "<cfcontet>" teria como eu utilizar uma formatação para que ele não seja impresso em formato hexadecimal!!
No screen name said on Jun 9, 2004 at 3:05 PM :
raas1,

Acho que ninguem sabem como a falar em portugues aqui.
No screen name said on Jul 6, 2004 at 9:57 PM :
I have try using the CFCONTENT example 3 posted by lbryngel. It did allow users to save into their harddisk in excel file extension but when users started to admend and update the downloaded file and saved it, it becomes a txt file. Anyone knows what is wrong??
G0V. said on Sep 20, 2004 at 8:10 AM :
This works fine on cf MX using several browsers from IE6 to Firefox 1.0 and Netscape 7.2 :

kuech said on Dec 31, 2003 at 11:20 AM :
The following should force the download of an Excel of Word document:

<cfheader name="Content-Type" value="unknown">
<cfheader name="Content-Disposition" value="attachment; filename=FileName.doc">
<cfcontent type="Application/Unknown" file="/http//downloads/FileName.doc" deletefile="No">
G0V. said on Sep 20, 2004 at 8:14 AM :
I used it like this to prevent to be able to download the file whitout being loged in, it also deletes the file after being created and downoaded, the file is actualy saved "outside" teh application root
<cfheader name="Content-Type" value="unknown">
<cfheader name="Content-Disposition" value="attachment; filename=#filename#.#ext#">
<cfcontent type="application/msexcel" deletefile="yes" file="#ExpandPath("../XL/#filename#.#ext#")#">
No screen name said on Oct 14, 2004 at 9:12 AM :
PDF ??

I have never been able to get the PDF download o work. Any suggestions or commentys?

thanks

Bill Richardson
Blacksburg VA
No screen name said on Oct 14, 2004 at 9:16 AM :
DOWNLOADING TO EXCEL

Many thaks to those who contributed ideas on how to get cfcontent to
work with Excel in IE. It works, now, but still has the .cfm name and type if the file opened by Excel. It appears the CFHEADED works to force Xcel to open the file, but still Xcel thinks it is a CFM file. This is why it gets saved as a text file, as another user commented.

Any ideas on how to get Xcel to open the downloaded file with the proper name and type?

thanks

Bill Richardson
Blacksburg VA
a.s.webster said on Oct 14, 2004 at 1:56 PM :
This is what I use in CFMX and IE 6.0.28xxx

<cfheader name="Content-Disposition" value="inline; filename=filename.xls">
<cfcontent type="application/vnd.ms-excel" file="d:\inetpub\wwwroot\temp\xyz1234.xls" deletefile="Yes">

It shows the files as filename.xls in excel.
No screen name said on Jan 11, 2005 at 10:20 PM :
I am developing an application which will download the contents of the report to an excel file.Everything is working fine.. But the thing is i have to add the company logo to the excel file. how can i add the logo from the webpage to the excel file being downloaded. Please help me as soon as possible.
Mandy35679 said on Jan 26, 2005 at 12:46 PM :
You can add the logo by referencing the entire url for your image not just the filename.
lgardne said on Feb 4, 2005 at 10:03 AM :
I am using a cfcontent tag to open an excel spreadsheet. My hyperlinks break because they change to the file///C:\<webdirectory>.
How can I maintain my original hyperlinks???
cmschofield said on Feb 7, 2005 at 3:39 PM :
IE 6.0 will try to save the .cfm file if the Content-Type is not specified as PDF while using <cfcontent>. I found that this happened only while using SSL. Thus, the following code correctly references the proper PDF. In our secure environment, the PDF files are dynamically created, displayed to the user, and then deleted.

<cfheader name="Content-Type" value="pdf">
<cfheader name="Content-Disposition" value="inline; filename=filename.ext">
<cfcontent type="application/pdf" file="filename.ext" deletefile="Yes">
perlyking said on Mar 3, 2005 at 8:55 AM :
How exactly does <cfcontent> work when streaming files back to the client? Is the file read into RAM as a whole, or are chunks read from disk and buffered?
This is an important issue for me becasue I need to stream large files (200mb+) and I don't want multiple concurrent downloads to kill the server.
No screen name said on Mar 3, 2005 at 10:06 AM :
For all those having problems with filename and formatting here goes a tip.
This is the way I usually use these tags:
<cfheader name="Content-Disposition" value="attachment; filename=OneMoreSpreadSheet.xls">
<cfcontent type="application/x-msexcel; charset=utf-16" reset="No">
Put attention to the charset used, I think that's the native format for excel. This way he gets the info the way he wants and no format or filename problems should appear.
Hope this helps.
TomDonovan said on Mar 8, 2005 at 7:34 AM :
re: How exactly does <cfcontent> work when streaming files ...

In CFMX <cfcontent> uses an 8kb buffer. It does not attempt to read the whole file into memory.

Note that one ColdFusion Simultaneous Request is occupied while a large file is sent using <cfcontent>.

For some applications, it makes more sense to download the file with <cflocation> rather than <cfcontent>. This lets the web server do the work rather than tying up a CF thread.
jrunrandy said on Mar 10, 2005 at 1:22 PM :
cmschofield's comment mentioned dynamically generating PDF files.

In ColdFusion MX 7, you can use the cfdocument tag or the cfreport tag
to generate PDF files dynamically.
cmschofield said on Jun 16, 2005 at 8:50 PM :
jrunrandy's comments about dynamic PDF generation

That's cool about CFMX 7 PDF generation. Excellent feature. However, I need to populate fields in existing PDF documents with data from a database.
xenden said on Aug 14, 2005 at 4:42 PM :
If you're using cfcontent, be sure you've got debugging output turned off (cfsetting showdebugoutput=false, I think) or else it will contaminate the file you are trying to export to the browser. If you can download the file via FTP but not via HTTP, that might be it. :)
psusemichel said on Aug 23, 2005 at 7:32 AM :
Regarding CFHeader & CFContent, if I use the "inline" setting for the "value" property, the name that is displayed is the CF page name, not the file name. For example:

<cfheader name="Content-Disposition" value="inline; filename=my_audio.mp3">
<cfcontent type="audio/mpeg" file="C:\Files\my_audio.mp3">

The Windows Media Player (WMP) opens and plays the file, however, the name that is displayed is "file_viewer.cfm" -- the name of my CF page. This seems to happen for Microsoft related applications -- MS Word, MS Excel, WMP, etc. However, PDF files and other non Microsoft files display the appropriate file name. Any ideas?
No screen name said on Nov 13, 2006 at 9:44 AM :
Regarding PDF documents and CF5:

When calling a page to download or inline-display a PDF include the following text in the query string

nocache=no

Not that I really understand why it works but it does.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-a17.htm