
Now that you are familiar with using Cold Fusion to output the contents of Web pages, you can investigate methods for generating non-HTML documents.
Your browser communicates with Web servers through a protocol called HTTP (Hyper Text Transfer Protocol). HTTP stipulates that before a Web server sends a file to your browser it must first transmit a character string, called a Content Type, indicating what kind of document you are about to receive. The browser then uses this information to correctly interpret the downloading information. In the vast majority of cases, your browser receives the Content Type "text/html" indicating that it is about to receive a text file containing HTML tags. However, you may want to transfer information in other formats, such as a Microsoft Excel-compatible spreadsheet, a Microsoft Word-compatible report, or a Postscript document.
You will be investigating methods of using Cold Fusion to dynamically create these types of documents as well as learning how to configure your browser to properly view them. The same techniques presented here will be used to create virtual worlds in Chapter 26, "Data-Driven VRML."
Back in the early days of the Internet, the contents of electronic mail were limited to ASCII text only. As the numbers of different computer systems connected to the Internet grew and the popularity of using electronic mail increased, a new standard was defined to allow for the cross-platform exchange of messages between these otherwise incompatible systems. This standard, named Multipurpose Internet Mail Extensions (MIME), includes support for non-textual information such as graphics, executable files, audio, and video. It also supports "multipart" messages which may combine several body parts (attachments), possibly of different types of data, into a single message.
MIME is an open, extensible protocol. The following example is an actual electronic mail message with attachments sent during the authoring of this book:
Return-Path: <ben@stoneage.com>
Received: from stoneage1.stoneage.com ([208.193.16.1]) by www.figleaf.com
(post.office MTA v1.9.3 ID# 0-13931) with ESMTP id AAA177
for <sdrucker@figleaf.com>; Tue, 6 Aug 1996 13:54:59 -0400
Received: from milliways.stoneage.com ([208.193.16.20])
by stoneage1.stoneage.com (post.office MTA v1.9.1 ID# 0-12155)
with ESMTP id AAA287; Tue, 6 Aug 1996 14:28:29 -0400
From: ben@stoneage.com (Ben Forta)
To: "Steve Drucker" <sdrucker@figleaf.com>,
Subject: Sample chapters and database
Date: Tue, 6 Aug 1996 13:28:47 -0400
X-MSMail-Priority: High
X-Priority: 1
X-Mailer: Microsoft Internet Mail 4.70.1132
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_01BB839B.32CBB0C0"
Content-Transfer-Encoding: 7bit
Message-ID: <19960806182825064.AAA287@milliways.stoneage.com>
This is a multi-part message in MIME format.
------=_NextPart_000_01BB839B.32CBB0C0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Hi,
Please find attached copies of two book chapters. This are unedited early
drafts, but they should give you an idea of the sample app and what it
includes.
Thanks.
-- Ben
---------------------------------------------------------------------
Ben Forta - Stoneage Corporation
E-Mail: ben@stoneage.com
Home page: http://www.stoneage.com
------=_NextPart_000_01BB839B.32CBB0C0
Content-Type: application/octet-stream; name="09708_6.doc"
Content-Transfer-Encoding: base64
Content-Description: 09708_6.doc (Microsoft Word Document)
Content-Disposition: attachment; filename="09708_6.doc"
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAOgAAAAAAAAAA
EAAAOwAAAAEAAAD+////AAAAADkAAAD/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
------=_NextPart_000_01BB839B.32CBB0C0
This message contains two parts, a text message describing the attached Microsoft Word document and the first few coded lines of the Microsoft Word document itself. Take special note of the Content Type headings. The first instance in the message informs the mail client that it has received a multipart message and defines a sequence of characters to use as a boundary to separate the text part of the message from the binary-encoded attachment. Later instances of Content Type describe the encoding of each individual section. Cold Fusion supports sending multipart e-mail through its MIMEATTACH option in the <CFMAIL> tag. You will now use the Cold Fusion <CFCONTENT> tag to dynamically set the Content Type header in order to generate and transfer non-HTML data to your Web browser.
The formal specification for MIME is available via the world wide Web at http://www.oac.uci.edu/indiv/ehood/MIME/MIME.html.
The MIME Content Type header field is used to describe the incoming data stream to the browser so that the data can be acted on in an appropriate manner. The header field is defined as [general type of data]/[specific format of data]. Thus, a browser receiving the line:
Content-Type: text/html
is notified that it is about to receive ASCII text formatted using the HTML specification so that it may render the document accordingly. Similarly, a Content Type of application/msword informs your browser that it is about to receive an application-dependent file in Microsoft Word format; your browser may, in turn, launch the Microsoft Word application to properly view the file.
There are currently seven pre-defined MIME categories. They are listed in Table 25.1.
| Category | Description |
| text | Textual information that without further enhancement or decoding would be readable by the user. No special software is required to display the data, although the specified character set must be supported by the client software. HTML documents belong in this category. |
| image | Graphical data that requires output on either a monitor or printer to be understood. Scanned photos in GIF format are part of this class. |
| video | A set of "moving images." The video may contain interlaced audio. Video files may require the use of additional hardware and software for proper viewing. Digitized movie trailers in Apple Quicktime format belong to this type. |
| audio | A file requiring a sound card and/or speakers to be heard. ABC News "broadcasts" its reports over the Internet in RealAudio format. |
| application | Other types of data, usually requiring the use of a specific software application to be properly decoded. Examples include a Microsoft Excel spreadsheet, compressed files in .ZIP format, Adobe PDF files, and executable program files. |
| multipart | Used almost exclusively for electronic mail applications, multipart data indicates that a single file is comprised of several independent body types or attachments, each with its own Content Type. An electronic mail message with an Excel spreadsheet attachment would be coded as a multipart data type. |
| message | An electronic mail message as an attachment to another electronic mail message. |
Each MIME category contains several subtypes, listed in Table 25.2. Entries in the form "x-subtype" indicate an experimental or local type. Later in this chapter you will create your own, private MIME type "text/x-csv-excel" in order to generate and properly format a Microsoft Excel compatible spreadsheet.
| MIME Type | MIME Subtype | Application |
| text | html | The most common MIME type on the Web, your standard HTML file |
| plain | Unformatted text | |
| image | gif | A graphic encoded in Graphic Interchange Format, readable by most Web browsers. |
| jpeg | A graphic compressed using JPEG lossy compression, readable by most Web browsers. | |
| tiff | A graphic encoded in Tagged Image File Format, usually requires an external viewer | |
| application | postscript | An output document in postscript format. |
| octet-stream | An executable program. | |
| A document in Adobe Portable Document Format. Requires a PDF viewer such as Adobe Acrobat or the Adobe Acrobat Amber plug-in. | ||
| msword | A document in Microsoft Word format. Requires additional software. | |
| zip | PKZIP archive compressed file. Requires WinZip or PKUNZIP utility to decode. | |
| audio | basic | A digitized sound sample (.au format), supported by most browsers. |
| x-pn-realaudio | A sound sample, compressed into RealAudio streamed format. | |
| Requires special software for both the client browser and Web server. | ||
| video | msvideo | Microsoft video file in .AVI format. |
| quicktime | Video file compressed using Apple Quicktime format. |
You can customize how your browser will respond when encountering a MIME document. These actions are limited to displaying the document within the browser, saving the document to disk, launching an external program to view the data, or asking the user to decide what action is to be performed. Every Web browser contains a database of MIME Content Types and file extensions. File extensions are necessary because some Web protocols such as FTP do not send Content Type headers. You will now define your own experimental Content Type in two of the most popular Web browsers.
You can access the Netscape Navigator 3.0 database of MIME types by selecting Options--General Preferences from the Netscape menu bar and then clicking on the Helpers tab. A table containing MIME types, file extensions, and actions should appear, similar to the one depicted in Figure 25.1.
Figure 25.1 The Helpers tab in Netscape Navigator 3.0 General Preferences allows you to customize your browser's MIME settings.
To add your experimental MIME type, click the Create New Type button in the Netscape Preferences window and type in the MIME type and MIME subtype, as shown in Fig- ure 25.2.
Figure 25.2 Netscape Navigator 3.0 lets you easily add your own custom MIME type.
In order to complete the process, you must fill in the file extension for your new MIME type and choose an action to be performed when your Cold Fusion generated file is received by the browser. If you have Microsoft Excel, select the Launch the Application radio button, click the Browse button and select Excel using the file dialog box. Your completed MIME definition should resemble Figure 25.3.
TIP: If you do not have Excel, you may download the free Microsoft Excel Viewer from www.microsoft.com.
Figure 25.3 Associate a file type extension with your MIME type and tell Netscape to launch the appropriate viewer when it is encountered.
All Microsoft Internet-enabled products (Internet Explorer) reference a database of MIME types that is stored in the Windows 95/NT registry. Unlike Netscape Navigator 3.0, which launches Microsoft viewers outside of the Navigator window, Microsoft Internet Explorer (MSIE) 3.0 integrates other Microsoft applications within an MSIE 3.0 frame or window. If you are creating an intranet application which outputs MIME to a Microsoft Office application, you may want to consider using Internet Explorer as your primary browser.
You can modify MIME actions for Microsoft applications through either the Windows 95/NT 4.0 Explorer or by editing the registry directly.
Modifying MIME Types Using Windows 95/NT Explorer. You can access the Microsoft
database of MIME types by selecting View-Options from the Windows Explorer menu bar,
and then clicking on the File Types tab. A dialog box, similar to the one depicted
in Figure 25.4, should appear on screen.
To add your experimental MIME type, scroll down the list of file types and select the option "Microsoft Excel Comma Separated..." Fill in the form as shown in Figure 25.5.
Modifying MIME Types by Using the Windows 95/NT Registry. You can also configure
MSIE, as well as any other Microsoft Internet clients for custom MIME types by editing
the Windows 95/NT 4.0 registry. You can start the registry editor, shown in Figure
25.6, by executing the file regedit32.exe (Windows 95/NT 3.51) or regedit.exe (Windows
NT 4.0).
Figure 25.4 The Options feature in the Windows 95/NT 4.0 Explorer allows you to customize MIME for all Microsoft Internet-enabled applications, including Internet Explorer.
Figure 25.5 You can associate a MIME Content Type with any Microsoft application file type.
Figure 25.6 The Windows 95/NT 4.0 registry editor allows you to link MIME types to file extensions.
File extensions are stored under the HKEY_CLASSES_ROOT key. Scroll down the list until you find the ".csv" file extension. Left click the .csv file folder. Settings for the .csv file type appears. If a Content Type does not exist for this subkey, you can add it by right-clicking on the .csv file folder and selecting New-String Value and filling in the form as depicted in Figure 25.6.
The <CFCONTENT> tag has two attributes. The mandatory TYPE attribute specifies which MIME Content Type is being returned by the current template. The optional FILE attribute dynamically inserts a file from the Web server into the output stream.
The following example demonstrates the use of <CFCONTENT> to output the image of an employee where the path to the image (i.e. C:\images\steve.jpeg) is stored in the field employees.photo:
<CFQUERY NAME="GetEmployeeImageFile" DATASOURCE="A2Zbooks">
SELECT photo FROM employees
WHERE employees.employeeid=#URL.employeeid#
</CFQUERY>
<CFCONTENT TYPE="image/jpeg" FILE="#GetEmployeeImageFile.photo#">
The HTML file format is wonderful as a cross-platform language for displaying text and graphics from within a Web browser. However, most desktop applications such as Microsoft Word and Microsoft Excel do not currently support this format, making it difficult for a user to export and edit Web-generated reports without resorting to cut-and-paste operations and re-typing. Fortunately, Cold Fusion through the <CFCONTENT> tag, allows you to output data in formats desktop applications can natively understand.
In the following example, you will build an interface, illustrated in Figure 25.7, that allows a user at your fictional company to save the exact same inventory report in three different file formats. The examples presented here use many of the techniques outlined in Chapter 23, "Advanced Form Techniques" (see Listings 25.1 and 25.2). The three examples rely on the same SQL select statement, contained in the file invquery.cfm, to generate the query result set.
<TITLE>A2Z Books - System Administration</TITLE> <FRAMESET ROWS="55,*"> <FRAME SRC="../common/menubar.CFM" NAME="menubar" noresize scrolling=no marginheight=3 marginwidth=3> <FRAMESET COLS="35%,65%"> <FRAME SRC="options.CFM" MARGINWIDTH=0> <FRAME SRC="../common/instructions.CFM" NAME="display" MARGINWIDTH=0> </FRAMESET> </FRAMESET> <NOFRAMES> <CFINCLUDE TEMPLATE="../common/noframes.CFM"> </BODY> </HTML> </NOFRAMES>
Figure 25.7 A frames-based interface allows the user to more easily distinguish the formatting differences between the file types.
<CFINCLUDE TEMPLATE="../common/header.CFM">
<CENTER><H2>Report Options</H2>
<FORM ACTION="/cgi/cf.exe/report.csv?template=/a2z/mime/example1/runreport.CFM"
target="display" METHOD="POST">
<TABLE BORDER=0>
<TR>
<TD>
<STRONG>Select Report</STRONG>
</TD>
<TD>
<SELECT NAME="ReportType">
<OPTION VALUE=1>Inventory Report
</SELECT>
</TD>
</TR>
<TR>
<TD VALIGN="TOP">
<STRONG>Report Format</STRONG>
</TD>
<TD>
<INPUT TYPE="RADIO" NAME="ReportFormat" VALUE="text/html" CHECKED>
HTML<BR>
<INPUT TYPE="RADIO" NAME="ReportFormat" VALUE="text/x-excel-csv">
Excel/Comma Delimited<BR>
<INPUT TYPE="RADIO" NAME="ReportFormat" VALUE="application/rtf">
MSWord/RTF<BR>
</TD>
</TD>
</TR>
</TABLE>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Run Report">
</FORM>
</BODY>
</HTML>
Using Cold Fusion to Set Default File Names. Many viewer applications examine the first few characters of the data file they are loading and try to make a decision about how to best represent the data on-screen. Certain file formats, such as RTF, contain coding that is functionally similar to the MIME Content Type, indicating to the viewer appli-cation how the incoming data should be interpreted. Other formats, such as comma-delimited text, do not contain such coding and thus may not be displayed properly. A secondary technique data viewers use is to represent data based on filename extension. Thus a comma separated format file named "report.txt" may not produce the desired results in Microsoft Excel; however, the same file, renamed "report.csv," with the ".csv" file extension properly associated in the Windows 95 explorer will be loaded properly. You can force Cold Fusion to assign a default filename and extension to a downloading file by using the method illustrated in Listing 25.3.
<CFIF #reportformat# contains "html"> <CFLOCATION URL="/cgi/cf.exe/report.html?template=/a2z/mime/example1/htmlreport.CFM"> </CFIF> <CFIF #reportformat# contains "excel"> <CFLOCATION URL="/cgi/cf.exe/report.csv?template=/a2z/mime/example1/csvreport.CFM"> </CFIF> <CFIF #reportformat# contains "rtf"> <CFLOCATION URL="/cgi/cf.exe/report.rtf?template=/a2z/mime/example1/rtfreport.CFM"> </CFIF>
Using Cold Fusion and <CFCONTENT> to Output HTML. Cold Fusion uses text/html as its default Content Type. Therefore, when generating a report in HTML format, issuing a <CFCONTENT> statement is unnecessary. Listing 25.4 demonstrates a method for outputting a list of inventory by using HTML tables to maintain a columnar format and CF to group books by category. The results are depicted in Figure 25.8.
<CFINCLUDE TEMPLATE="invquery.CFM"> <CFINCLUDE TEMPLATE="../common/header.CFM"> <H2>Available Inventory</H2> <UL> <CFOUTPUT QUERY="getinventory" GROUP="category"> <LI><H3>#getinventory.category#<H3></LI> <TABLE BORDER=1> <TH><H5>Book ID</H5></TH> <TH><H5>ISBN</H5></TH> <TH><H5>Title</H5></TH> <TH><H5>Publisher</H5></TH> <TH><H5>Location</H5></TH> <CFOUTPUT> <TR> <TD>#Bookid#</TD> <TD>#ISBN#</TD> <TD>#Title#</TD> <TD>#Publisher#</TD> <TD>#Location#</TD> </TR> </CFOUTPUT>
</TABLE> </CFOUTPUT> </UL> </BODY> </HTML>
Figure 25.8 By using Cold Fusion and HTML you can generate a read-only "preview" of the data to be exported.
Using Cold Fusion and <CFCONTENT> to Output Comma Separated Values (CSV).
Comma separated value files (CSV) are used principally for the exporting and
importing of data into a spreadsheet or database table. This format uses the comma
character (,) as a separator to delineate column data, and a carriage return/line
feed to indicate the presence of a new row. In the following code listing, line 3
outputs the report title "Available Inventory." Line 4 sets up column headings,
and your CFOUTPUT query dynamically generates columns of inventory information on
lines 5-6. The .CSV result, loaded into Microsoft Excel, is shown in Figure 25.9.
NOTE: Cold Fusion strips all text output which occurs in a template before the occurrence of the <CFCONTENT> tag.
There are several issues to keep in mind when attempting to output a non-HTML file type. All text following the <CFCONTENT> tag will be output as plain ASCII. Therefore, HTML tags such as <H1>..</H1> and <STRONG>..</STRONG> will not have the desired effect on the output file. Remember, we're no longer generating code for a Web browser to interpret. Our only concern must be how the helper application will interpret the data in the output file. Also, any tabs or carriage returns inserted after the <CFCONTENT> tag will be inserted into your result set. For example, the following code snippets are not functionally equivalent:
<CFOUTPUT QUERY="getinventory">#Category#,#Bookid#,#ISBN#,#Title#,#Publisher# </CFOUTPUT> <CFOUTPUT QUERY="getinventory"> #Category#,#Bookid#,#ISBN#,#Title#,#Publisher# </CFOUTPUT>
In the prior example, the second code listing would insert an extra carriage and line feed into your generated file.
<CFINCLUDE TEMPLATE="invquery.CFM"> <CFCONTENT TYPE="text/x-excel-csv">Available Inventory Category,Book ID,ISBN,Title,Publisher,Location <CFOUTPUT QUERY="getinventory"> #Category#,#Bookid#,#ISBN#,#Title#,#Publisher#,#Location# </CFOUTPUT>
Figure 25.9 Microsoft applications, such as Excel, can be configured to execute from within an Internet Explorer frame when viewing MIME documents.
Using Cold Fusion and <CFCONTENT> to Output Rich Text Format. Rich
Text Format (RTF), the native format for Windows Help Files, is supported by nearly
all windows word processors. Rich Text constructs are similar in format to HTML--a
series of tags denote style settings and formats. In the following example, you first
prototype a document in Microsoft Word, inserting a graphic and defining a specifications
table, and then save the document in Rich Text Format. By analyzing the resulting
file, and through a process of trial and error, you can determine the appropriate
placement of <CFOUTPUT> tags within the file. To make the following program
listing more concise, you place the top of the document, containing a bitmap, into
a separate file and used <CFINCLUDE> to import it at execution time. Listing
25.6 demonstrates an implementation of this methodology, while Figure 25.10 depicts
the resulting file, loaded into Microsoft Word.
<CFINCLUDE TEMPLATE="invquery.CFM">
<CFCONTENT TYPE="application/rtf"><CFINCLUDE TEMPLATE="rtfhdr.rtf">
<CFOUTPUT QUERY="getinventory" GROUP="category">
{\b\fs24 #category#
\par }\trowd \trgaph108\trleft-108 \clbrdrt\brdrs\brdrw30\brdrcf1
\clbrdrb\brdrs\brdrw15\brdrcf1 \clcfpat1\clcbpat8\clshdng6000
\cellx1663\clbrdrt\brdrs\brdrw30\brdrcf1 \clbrdrb\brdrs\brdrw15\brdrcf1
\clcfpat1\clcbpat8\clshdng6000 \cellx3434\clbrdrt
\brdrs\brdrw30\brdrcf1 \clbrdrb\brdrs\brdrw15\brdrcf1
\clcfpat1\clcbpat8\clshdng6000 \cellx5205\clbrdrt\brdrs\brdrw30\brdrcf1
\clbrdrb\brdrs\brdrw15\brdrcf1 \clcfpat1\clcbpat8\clshdng6000
\cellx6976\clbrdrt\brdrs\brdrw30\brdrcf1 \clbrdrb
\brdrs\brdrw15\brdrcf1 \clcfpat1\clcbpat8\clshdng6000 \cellx8747
\pard \widctlpar\intbl
{\b\fs24\cf8 Book\cell }
{\fs24\cf8 ISBN\cell Title\cell Publisher\cell Location\cell }\pard
\widctlpar\intbl {\fs48\cf8 \row }\trowd \trgaph108\trleft-108
\clcfpat1\clcbpat8\clshdng3000
\cellx1663\cellx3434\cellx5205\cellx6976\cellx8747 \pard
<CFOUTPUT>
\widctlpar\intbl
{\b\fs24 #title# \cell }
{\fs24 #isbn#\cell #title#\cell #publisher#\cell #location#\cell }\pard \widctlpar\intbl {\fs48 \row }\trowd \trgaph108\trleft-108 \clbrdrb
\brdrs\brdrw30\brdrcf1 \clcfpat1\clcbpat8\clshdng3000 \cellx1663\clbrdrb\brdrs\brdrw30\brdrcf1 \cellx3434\clbrdrb\brdrs\brdrw30\brdrcf1 \cellx5205\clbrdrb\brdrs\brdrw30\brdrcf1 \cellx6976\clbrdrb\brdrs\brdrw30\brdrcf1 \cellx8747
\pard \widctlpar\intbl\pard
</CFOUTPUT>
}
</CFOUTPUT>
Figure 25.10 Rich Text Format, while more difficult to understand than HTML, enables you to generate complex reports which are editable in any Windows word processor.
Cold Fusion allows you to take virtually any file format with column definitions and use MIME to reformat the information for a helper application. The O'Reilly Web site Web server does not currently include a Web-based administration interface. Through some clever manipulation of Cold Fusion, you can build an interface to make the Web site access log file available from a Web browser.
The Web site Windows-format access log is natively formatted as a tab-delimited file. In the following example you will save the log to a Microsoft Access memo field, and then use <CFCONTENT> to provide a Web-based method to view it.
Setting Up the Example. The data requirements for this example are summarized
in Table 25.3.
| Field Name | Data Type | Description |
| log_id | autonumber | Primary key, unique identifier |
| log_date | date/time | Time and date the log is saved |
| log_contents | memo | Where you store the tab-delimited Web site log |
Once again you use the same frames-based interface, detailed in Listing 25.7, depicted in Figure 25.11, and first introduced in the prior example.
<TITLE>A2Z Books - System Administration</TITLE> <FRAMESET ROWS="55,*"> <FRAME SRC="../common/menubar.CFM" NAME="menubar" noresize scrolling=no marginheight=3 marginwidth=3> <FRAMESET COLS="35%,65%"> <FRAME SRC="options.CFM" MARGINWIDTH=0> <FRAME SRC="../common/instructions.CFM" NAME="display" MARGINWIDTH=0> </FRAMESET> </FRAMESET> <NOFRAMES> <CFINCLUDE TEMPLATE="../common/noframes.CFM"> </BODY> </HTML> </NOFRAMES>
Figure 25.11 By using Cold Fusion you can save a Web site access log into a database for later viewing.
Cycling the O'Reilly Web Site Access Log. The Web site Web server constantly
saves access information to a file named access.log. The Web site keeps a constant
file lock on this file, thereby making it inaccessible to Cold Fusion, but provides
support for a special URL (/~cycle.acc) which, when accessed, causes the server process
to "cycle" the log. The current log is renamed access.001 and the Web site
creates a new access.log file. Listing 25.8 demonstrates the method for initiating
the cycle process.
<CFQUERY NAME="getlogs" DATASOURCE="a2zdata">
SELECT * FROM websitelog order by log_id
</CFQUERY>
<CFINCLUDE TEMPLATE="../common/header.cfm">
<CENTER>
<H2>Website Access Log Options</H2>
Please select from the following options
</CENTER>
<UL>
<LI><FONT SIZE="+1">Cycle & Save Options</FONT><BR>
<FORM ACTION="/~cycle-acc" METHOD="POST" TARGET="display">
<INPUT TYPE="SUBMIT" VALUE="Cycle Access Log"><BR>
</FORM>
<FORM ACTION="savelog.cfm"
METHOD="POST" TARGET="display">
<INPUT TYPE="SUBMIT" VALUE="Save Access Log">
</FORM>
<LI><FONT SIZE="+1">Log Options</FONT><BR>
<CFIF #getlogs.recordcount# is not 0>
<FORM ACTION="viewform.cfm"
TARGET="display" METHOD="POST">
<SELECT NAME="Logid">
<CFOUTPUT QUERY="getlogs">
<OPTION VALUE=#log_id#>#dateformat(log_date)#
</CFOUTPUT>
</SELECT>
<INPUT TYPE="SUBMIT" NAME="LogAction" VALUE="View">
<INPUT TYPE="SUBMIT" NAME="LogAction" VALUE="Delete">
<BR>
</FORM>
<CFELSE>
Sorry, no logs are available for display.
</CFIF>
</UL>
</BODY>
</HTML>
Saving the Access Log into a Database Table. Your algorithm requires the Webmaster to make the directory containing Web site access logs available as a CF template directory. Once the access.001 file has been created, you can use the <CFINCLUDE> tag to import the file into an HTML form text area and ask the user to save the information into an Access memo file. Once the information has been saved into Access, you use <CFFILE> to delete the now-redundant access.001 file and update your log selection form. Listing 25.9 demonstrates the technique for reading in the log to a form textarea, while Listing 25.10 details the save and delete processes.
<CFINCLUDE TEMPLATE="../common/header.cfm"> <FORM ACTION="writelog.cfm" METHOD="POST"> <CENTER> <H2>Website Log Contents</H2> <TEXTAREA NAME="log_contents" ROWS=5 COLS=60> <CFINCLUDE TEMPLATE="/website/access.001"> </TEXTAREA> <BR><BR> <INPUT TYPE="SUBMIT" NAME="action" VALUE="Save Log"> <INPUT TYPE="SUBMIT" NAME="action" VALUE="Discard Log"> </CENTER> </FORM> </BODY> </HTML>
<CFIF #action# contains "Save">
<CFINSERT
DATASOURCE="a2zdata"
TABLENAME="websitelog"
FORMFIELDS="log_contents"
>
<CFFILE ACTION="DELETE" FILE="E:\website\logs\access.001" >
<CFELSE>
<CFFILE ACTION="DELETE" FILE="E:\website\logs\access.001" >
</CFIF>
<CFINCLUDE TEMPLATE="../common/header.cfm">
<SCRIPT>
function refreshoptions() {
top.options.location.href="options.cfm"}
</SCRIPT>
<CFIF #action# contains "Save">
<BODY ONLOAD="refreshoptions()">
<CENTER>
<BR>
<H2>Your log file has been saved</H2>
</CENTER>
<CFELSE>
<CENTER>
<H2>Your log file has been discarded</H2>
</CENTER>
</CFIF>
</BODY>
</HTML>
Displaying the Access Log as a MIME Type. Once you have the Web site log contained within the Access table, displaying it using <CFCONTENT> becomes a trivial exercise. Note in Listing 25.11 that you have once again defined an "experimental" MIME type named "text/x-tabdelimited". For the example to generate the results shown in Figure 25.12, your browser needs to be configured to recognize this Content Type.
Figure 25.12 The final result: a nicely formatted Web site access log displayed within Microsoft Excel.
NOTE: The spaces in between the headers are actually tab characters. In this example, tab characters are required for Excel to correctly parse the table.
<CFQUERY NAME="getlog" DATASOURCE="a2zdata"> SELECT * FROM websitelog where log_id=#logid# </CFQUERY> <CFIF #getlog.recordcount# is 1>
<CFIF #logaction# contains "View">
<CFCONTENT TYPE="text/x-tabdelimited">Website Access Log - Saved <CFOUTPUT>#dateformat(getlog.log_date)#</CFOUTPUT>
Date/Time IP Address Authentication Server Realm User Name Action Bytes Transferred w/header Bytes Transferred w/o header
<CFOUTPUT QUERY="getlog">#log_contents#</CFOUTPUT>
<CFELSE>
<CFINCLUDE TEMPLATE="../common/header.cfm">
<CENTER>
<BR>
<H2>Delete Log</H2>
<CFOUTPUT>
<STRONG>
Please confirm: Delete access log saved on #dateformat(getlog.log_date)#
at #timeformat(getlog.log_date)#?<BR>
</STRONG>
<FORM ACTION="delete.cfm?log_id=#logid#"
METHOD="POST">
<INPUT TYPE="SUBMIT" NAME="action" VALUE="Delete">
<INPUT TYPE="SUBMIT" NAME="action" VALUE="Cancel">
</FORM>
</CFOUTPUT>
</BODY>
</HTML>
</CFIF>
<CFELSE>
<CFINCLUDE TEMPLATE="../common/header.cfm">
<CENTER>
<BR>
<H2>Sorry, this log has been deleted!</H2>
</BODY>
</HTML>
</CFIF>
Despite the shameless evangelizing of <CFCONTENT> in this chapter, there are instances when it is more efficient to not use Cold Fusion as a mechanism for transmitting MIME. As part of the HTTP specification, all Web servers insert a MIME Content Type header into every document served. Web servers handle MIME mappings in a method similar to browsers--they rely on file name extensions as a method for determining which Content Type header to use. Also like browsers, every Web server keeps an extensible database relating file extensions to Content Types.
In instances when you want to transmit a static, pre-existing document, such as an Adobe PDF file, you can use either of the following two methodologies:
Method 1--Using Cold Fusion to transmit a static pdf file named "pdffile.pdf" requires a separate .CFM file containing the following code:
<CFCONTENT TYPE="application/pdf" FILENAME="E:\pdf\pdffile.pdf">
Method 2--Using your built-in Webserver functionality does not require a separate .CFM file. A simple HTML anchor reference does the job:
<A HREF="/pdf/pdffile.pdf">Click here to download the PDF file</A>
TIP: You may want to invoke method 1 if you have security concerns about making your file available via a publicly accessible URL.
In most cases, method 2 is clearly a better choice. It requires less overhead and is easier to maintain.
In the following sections, you will learn how to define custom Content Type mappings for your Web server.
NOTE: For every Content Type mapping defined on your Web server there should exist an identical mapping on the client's browsers. Remember to inform your users when any content mapping change takes place so that they can update their browsers.
The O'Reilly Web site software provides a Windows interface, shown in Figure 25.13, for editing MIME Content Types. To add a new Content Type, you must follow these steps:
Figure 25.13 Configuring O'Reilly Web site MIME types using their windows administration is simple.
The Microsoft Internet Information Server (IIS) stores its MIME type information in the Windows NT registry, depicted in Figure 25.14. To add a new Content Type, you must follow these steps:
The Netscape Enterprise Server stores its MIME database as an ASCII text file as depicted in Figure 25.15. To add a new Content Type, you must observe the following steps:
Figure 25.14 The Microsoft Internet Information Server stores MIME types as registry keys.
Figure 25.15 Netscape Enterprise Server keeps a database of MIME types in a text file.
In this chapter, you learned that developing CF applications involving MIME types requires not only an understanding of the MIME Content Type specification but also considerable coordination between CF, your web server, and your client's browsers. As the race to develop more varied content on the web continues, expect to see new MIME types and helper applications emerge. Some of these new data formats you may eventually want to incorporate into your own application. With your knowledge of the MIME extensible framework and <CFCONTENT> you'll be prepared!
For more information about the techniques presented here, please refer to the following chapters:
© Copyright, Macmillan Computer Publishing. All rights reserved.