BackUp LevelNext

Returning MIME Content Types (CFCONTENT)

MIME (Multipurpose Internet Mail Extensions) was created for several purposes, one of which was to create an explicit link between data files and applications that are used to view and edit the files. MIME accomplishes this by providing a content type specification with each data file.

On the Web, the exchange of data happens using Hypertext Transport Protocol (HTTP). Both the Web server and the Web client must agree on the content type of the file, and either the client must be able to understand files of that format, or the client needs to find an application that can use the files.

By default, the content type ColdFusion uses when it generates data for the browser is text/html. This is simply an HTML file type. Despite the fact that text/html is the most frequently used content type on the Web, there might be instances when you want to use ColdFusion to dynamically generate documents of other content types.

Example: Returning VRML

To dynamically return a different VRML (Virtual Reality Modeling Language) model based on a parameter passed in the URL, use the syntax:

<CFCONTENT TYPE="x-world/x-vrml">
<CFQUERY NAME="GetCyberCafeRoom" 
DATASOURCE="CyberCafe">
SELECT VRML_Script
    FROM CyberCafeRooms
    WHERE FloorNumber=#URL.FloorNumber#
</CFQUERY>

<CFOUTPUT QUERY="GetCyberCafeRoom">
    #VRML_Script#
</CFOUTPUT>

An important issue to consider when sending non-HTML content types to the client is the browser's ability to understand the data type. All major browsers come with a set of predefined content types, which are either supported by the browser itself or by an auxiliary application often referred to as a viewer. Therefore, it's important that you notify a user in advance when you intend to use a content type that may not be directly supported by their browser.

Example: Populating an Excel spreadsheet

The following example demonstrates a way to use ColdFusion to dynamically generate a spreadsheet that can load directly into Microsoft Excel. In order to do this seamlessly, you must associate the text/tabdelimited content type with your local Microsoft Excel application:

<CFCONTENT TYPE="text/tabdelimited">
<CFQUERY NAME="GetFinancialData" 
    DATASOURCE="MutualFunds">
    SELECT *
    FROM Funds
    WHERE Fund_ID=#Fund_ID#
</CFQUERY>
<P>Month Value Percentage Gain Estimate</P>
<CFOUTPUT QUERY="GetFinancialData">
#Month# #Value# #PercentageGain# #Estimate#
</CFOUTPUT>

The spaces between fields and headers are actually tab characters. Tab characters are required in order for Excel to correctly parse the columns in the table.

Note

ColdFusion strips all text output that occurs in an application page before the occurrence of the CFCONTENT tag.

Tip

If you just need to quickly add a range of cells from a spreadsheet to a page, you can copy the range and paste it into ColdFusion Studio's Design window. The data is automatically converted to an HTML table.

Example: Returning a file

The following example illustrates how to use CFCONTENT to return a file from the web server. The advantage of this scenario is that you can control access to files. Using CFCONTENT with the FILE attribute allows you to return files that are not stored in the root of your Web server document directory.

<CFCONTENT TYPE="text/tabdelimited" 
    FILE="c:\land\topo.txt">

Note

ColdFusion generates an error if it finds any output after the CFCONTENT in this type of file retrieval operation.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.