主要用于以下操作:
如果要限制这个标签, 在 ColdFusion Administrator > Security > Sandbox Security 中设定.
数据输出标签
<cfcontent
deleteFile = "yes|no"
file = "filename"
reset = "yes|no"
type = "file type"
variable = "variable name">
|
属性 |
必须/可选 |
默认 |
描述 |
|---|---|---|---|
|
deleteFile |
可选 |
no |
只适用于当你用file属性指定了一个文件的时候.
|
|
file |
可选 |
|
内容里提供了页面输出的文件的名字。文件名是以一个盘符和冒号,或者是向前或向后斜线开头的。当你在一个分布式配置里使用ColdFusion 的时候,文件的属性必须涉及到一个网络服务器运行的系统上的路径。当你使用这个属性的时候,在当前CFML 页面上的其它输出就会被忽略;只有当前的文件被送往客户端. |
|
reset |
可选 |
yes |
如果你指定了一个文件或者变量属性,这个属性就没有作用;不然的话,它会这样运作:
|
|
type |
可选 |
|
页面的MIME 内容类型,可选是否用";"跟上对字符的编码.默认的情况下, ColdFusion在UTF-8 character编码上把页面作为text/html内容类型来发送. 然而, 如果file属性被指定, ColdFusion 会尝试从文件中获取内容类型. 内容的类型决定了浏览器或者客户端是如何交互页面内容的。. 下面是可以被使用的内容类型:
以下是可以用于跟在类型后面定义的文字编码:
例如: type = "text/html" type = "text/html; charset=ISO-8859-1" |
|
variable |
可选 |
|
以Coldfusion 二进制变量命名的,用于显示在浏览器前的内容,像cfchart生成的内容或是cffile action="readBinary"产生的pdf或excel文件 . 当你使用这个属性时其他在当前CFML页面输出的内容都被屏蔽;只有这个内容的文件被传送到客户端. |
设定输出的字符编码,包含在html页面上,如下所示:
<cfcontent type="text/html; charset=ISO-8859-1">
当ColdFusion 运行一个HTTP要求的时候,它决定了HTTP response 返回的数据的字符编码。默认的方式下,ColdFusion 使用Unicode UTF-8 格式返回数据,同时它忽略页面上一个HTML meta 标签的值。你可以使用cfcontent 标签来覆盖默认的字符编码响应。举个例子来说,如果要告诉ColdFusion 返回使用了Japanese EUC 字符编码 的页面,你可以这样使用类型标签:
<cfcontent type="text/html; charset=EUC-JP">
如果在一个自定义标签中调用 cfcontent 标签, 而且你不想在它被其他程序或者自定义标签调用的时候让标签抛弃当前的页面,你可以设置reset = "no".
如果文件删除操作不成功, ColdFusion 会找到这个错误.
不要在一个页面的 cfflush标签后使用这个标签, 它将会没有效果或 ColdFusion 抓不到这个错误.
下面的标签可以让绝大多数的浏览器显示一个对话框提问使用者是否他们要保存用cfcontent 标签指定的文件,而cfcontent 标签使用了文件名的值指定的文件名。如果使用者选择打开文件,绝大多数的浏览器都会打开相关的程序里的文件,而不是浏览器窗口
<cfheader name="Content-Disposition" value="attachment; filename=filename.ext">
有一些文件的类型,比如说PDF 文件,不使用可执行的代码,还可用在绝大多数的浏览器上显示。为了要求浏览器直接显示文件,可以使用一个cfheader标签,代码类似如下
<cfheader name="Content-Disposition" value="inline; filename=name.ext">
你可以使用任何值作为文件名属性的filename 部分,但是文件类型的ext 部分就必须是标准 windows 扩展.
对于包含了可执行代码的文件类型,比如说Microsoft Excel 文件,绝大多数的浏览器总是在打开文件之前询问。对于这些文件类型,如果使用者选择打开文件的话,内置内容配置规格要求浏览器直接显示文件.
更多字符编码的信息,请看下面的页面信息:
For a complete list of media types used on the Internet, see www.iana.org/assignments/media-types/.
<!--- 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:\CFusionMX7\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>