View comments | RSS feed

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>

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

Version 6

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

Comments


hlichtin said on Mar 10, 2003 at 2:36 PM :
A MAcromedia bug has been entered against the problem reported by faceless.
h2oman said on Jun 21, 2002 at 3:17 PM :
use cfheader name="Content-Disposition" value="attachment; filename=filename.ext"
to force browsers to save the file as an attachment and with a specific filename.

All Mac IE versions (up to and including IE 5.1 for OS9/X) currently ignore the attachment specification. No solution from Microsoft has been made available, nor do they confirm existence of the problem.
ctina said on Jul 19, 2002 at 6:59 PM :
Update from Macromedia, in response to h2oman, re cfcontent and attachments:
Thanks for your comment and tip. We'll consider adding it to the next release of the documentation.
Christina Lamkin, CF documentation team
sundvor said on Aug 15, 2002 at 5:46 AM :
Under type, it is listed that the ; delimiter is optional. However, omitting the semicolon makes my IE6 on Windows XP display a "download file" dialogue box instead of displaying the page.

The following will correctly format a page using ISO-8859-1:
&lt;cfcontent type="text/html; charset = ISO-8859-1"&gt;

I got into this when researching why posting the Norwegian characters Æ, Ø and Å ended up like gibberish (å = "Ã¥r") when doing a cfdump var="#form#".

It turned out IE6 auto-detected the encoding as UNICODE (UTF-8), but when posting the page for some reason CFMX does not detect that UNICODE is being posted and tries to read it as something else. Setting the page encoding to ISO-8859-1 is probably a short term solution, but at least it works. I'd be interested in learning why IE6 & CFMX with Norwegian characters produces the mismatch though.
mikv said on Sep 10, 2002 at 2:11 PM :
The cfcontent solution to correctly submit special characters in a form does not work completely. The EURO symbol (for the benefit of you guys on the west side of the Atlantic, that's the symbol which represents the currency that most Europeans now carry around in our pockets), causes problems... ie a JRun error. Has anyone come accross this problem yet, and if so, does anyone have a solution?
sandeep_chheda said on Dec 16, 2002 at 3:52 AM :
I am having CF 4.5. I wanna show dynamic contents on the browser in a excel sheet format and then should be able to download that sheet. I tried the cfcontent tab but its just showing the download window and the it stops. I am actually not able to view the sheet on the browser. Can you help me in this as I have seen this happpen in MX so I dont know whether its possible in CF 4.5.

If you can send the code the location where my excelfile should be etc. That will be of great help.
faceless said on Feb 26, 2003 at 4:54 PM :
I've only just started trying to port our application to ColdFusion, but I appear to have hit a minor glitch when setting the content-type to "text/xml; charset=UTF-8"

If I set my CFM to this:

<cfsetting enablecfoutputonly="yes" showdebugoutput="no">
<cfcontent type="text/xml">
<cfoutput><?xml version="1.0"?>
<emptytag/>
</cfoutput>

It works perfectly and the Content-Type of the returned page is text/xml. If I change the cfcontent line to

<cfcontent type="text/xml; charset=UTF-8">

the Content-Type is suddenly switched back to "text/html; charset=UTF-8".

The reason for this appears to be the calls made by the CfmServlet. In the first example, the calls are:

setContentType("text/html; charset=UTF-8")
setContentType("text/xml")
setLocale(Locale.US)
setHeader("Content-Language", "en-US")
setLocale(Locale.US)
setHeader("Content-Language", "en-US")
getWriter()

whereas with the second, it's

setContentType("text/html; charset=UTF-8")
setContentType("text/xml; charset=UTF-8")
setLocale(Locale.US)
setHeader("Content-Language", "en-US")
setContentType("text/html; charset=UTF-8")
setLocale(Locale.US)
setHeader("Content-Language", "en-US")
setContentType("text/html; charset=UTF-8")
getWriter()

Note the additional later calls to setContentType.

As it happens this isn't a big deal, because the default content-type of XML is UTF-8 anyway, and as ColdFusion defaults to UTF-8 it works. It also only happens with UTF-8 - <cfcontent type="text/xml; charset=UTF-16"> works fine.

In short? If you need to specify a <cfcontent> type of "text/xml; charset=UTF-8", just use "text/xml" on it's own, it has the same effect.

Len said on Aug 9, 2003 at 5:01 PM :
Does this cfcontent and cfheader work? I am trying to create an excel spread sheet. I have followed your example in the CF, but I don't get new lines. The data comes out in the first cell. Also, I am not sure if I am getting a tab or a space as a separtor per the example. Does this sound familar? I am using MX
mliving said on Aug 22, 2003 at 2:11 PM :
I too run MX ... here is an example in how I do it:
<CFOUTPUT QUERY="detail_query">#field1#,#field2#,#replace(field3,",",";","all")##chr(13)#</CFOUTPUT>

The chr(13) is what tells it to go to a new line. The replace will replace commas with semicolons so I don't go to a new column in excel before the end of the field.

However, I am trying to find out how to handle a number of blank spaces within a field. When I have a larger number of repeating spaces in my user defined text excel will start a new row which I am trying to correct. Any ideas?
No screen name said on Dec 18, 2003 at 11:55 AM :
I've used this method successfully on my development server. I'm prompted to save the file using the correct name "DMCReports.xls"
But when I run the same code on my production server, when the client side is prompted to save the file in the File DownLoad window I get the file name "index.cfm?action=RunReport" which is the href for the link to run this report. If I select Open or Save I get an Error stating the file cannot be downloaded. (no wonder with a name like that!). The two servers are running CFMx 6.1, and cfcontent is enabled.

Does anyone have any ideas why this should work on one machine and not another???

the code is :
<cfheader name="Content-disposition" value="attachment; filename=DMCReport.xls">
<cfcontent type="application/vnd.ms-excel">

ps - I've tried flipping the order of the cfheader and cfcontent lines - same issue

thanks!
lmr1010 said on Feb 4, 2004 at 8:31 AM :
I'm having a similar problem. On the production server (CF5) my page displays an excel spreadsheet in the browser. On two development servers (one CF5, the other CFMX), it tries to download the page and gives the error message "Internet Explorer cannot download <filename> from <name of server>. Internet Explorer was unable to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later". Here is the code:
<cfcontent type="application/msexcel">
<cfheader name="Content-Disposition" value="filename=GetComments.xls">

I'm using IE6, not that it matters, since I use the same browser to test all the servers.

And ideas?? Thanks!
Lisa
cfdawna said on Apr 5, 2004 at 6:12 AM :
I am having a similiar problem with cfheader and cfcontent.
On the development servers the tags work fine. On the production server I get the error message "Internet Explorer cannot download <filename> from <name of server>. Internet Explorer was unable to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later". Here is the code:
<CFHEADER NAME="content-disposition" VALUE="attachment; filename=#Filespec#.xls">
<CFCONTENT type ="application/vnd.ms-excel" reset="yes">
The systems people tell me that both servers are running the same version of ColdFusion MX.
Any help would be appreciated.
big_web_monkey said on May 22, 2004 at 4:38 PM :
I need help! I am using cfcontent to serve up different files by passing the file name and mime type from a database. The problem is that it works fine on Windows 2000, but not on Windows XP (Both running IE6). When you try to get to the file thru IE6 on a windows xp machine I get an alert box telling me that "Adobe Reader could not open 'pa037525.fdf' because it is either not a supported file type or because the file has been corrupted." The string 'pa037525' is different everytime I try to get to a file. I am not using cfheader on the page.

Has anyone else had this problem with Windows XP and cfcontent?

-Rosario
ASandstrom said on May 24, 2004 at 6:11 AM :
LiveDocs is for documentation comments only and does not receive enough traffic to handle questions such as this. I suggest posting your issue to the online forums: http://webforums.macromedia.com/coldfusion/
damion said on Sep 13, 2004 at 4:09 PM :
People have cfheader above cfcontent in examples above. Does parameter "reset" in cfcontent wipe out the cfheader output, or does reset pertain only to content in the html document?
halL said on Sep 14, 2004 at 7:36 AM :
The reset option should not affect HTTP headers generated by cfheader. It only pertains to the html document contents.
stroms1 said on Mar 10, 2005 at 12:51 PM :
This is in response to the issue of "Internet Explorer cannot download ". I was also only having this problem on our Production server. The only difference between the other environments and our Production environment was it ran SSL. This link explains why this error occurs http://support.microsoft.com/default.aspx?scid=kb;en-us;316431 .
AWKenger said on Jul 31, 2007 at 1:11 PM :
In response to the IE problem above I too had the same problem in an SSL
environment. Adding the following two lines at the top fixed the problem.

<cfheader name="Pragma" value="">
<cfheader name="Cache-control" value="">

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt118.htm