【问题标题】:Download CFM page as Word Doc将 CFM 页面下载为 Word Doc
【发布时间】:2021-06-28 20:22:20
【问题描述】:

我正在尝试使用 cfinclude 将 .cfm 的输出呈现为 Word 文档,如下所示:

<cfheader name="content-disposition" value="attachment; filename=""MyDocument.doc""" />
<cfcontent type="application/msword" reset="true">
<cfinclude template="PageToDownload.cfm">

由于cfinclude 将 .cfm 输出为 html,理论上这应该可行。但是,这样做会下载一个文档,该文档在 Word 中出现“文本/xml 声明可能仅在输入的最开始出现”错误。我用记事本++检查了下载的文档,并在顶部看到了这个:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
...

如果我删除空行、?xml 和 !DOCTYPE(基本上删除了直到 &lt;html 的所有内容),此文档将在 Word 中打开。是否可以去掉 xml 和 DOCTYPE 标签并使用cfinclude&lt;html 开始?或者,我应该采取其他方法吗?

【问题讨论】:

  • 我会做.docx,因为它是 XML 的 zip,但事件会很耗时。保存 HTML 并试图让它伪装成 .doc 似乎是在自找麻烦。
  • 如果不需要,为什么不直接从“PageToDownload.cfm”中删除&lt;?xml version="1.0" encoding="UTF-8"?&gt;,因为该脚本似乎是将其添加到输出中?
  • @SOS 这是我正在开发的程序的简化版本。在我的情况下,“PageToDownload”用于需要该 xml 标记的其他功能,所以这不是一个选项

标签: coldfusion document


【解决方案1】:

使用您的技术,您可以使用&lt;cfsavecontent&gt;PageToDownload.cfm 的内容放入一个变量中。一旦内容在所述变量中,您可以在打开 &lt;html&gt; 标记之前删除所有内容,然后在 &lt;cfheader&gt;&lt;cfcontent&gt; 语句之后删除 &lt;cfoutput&gt; 该变量。

您的代码可能看起来像这样。

<!--- Save contents of file to download into fileContents variable --->
<cfsavecontent variable="fileContent">
    <cfinclude template="PageToDownload.cfm">
</cfsavecontent>

<!--- Strip out contents prior to the opening <html> tag --->
<cfset pos = findNoCase("<html", fileContent)>
<cfset fileContent = mid(fileContent, pos, 1000000)>

<!--- Output fileContent after the header statements --->
<cfheader name="content-disposition" value="attachment; filename=""MyDocument.doc""" />
<cfcontent type="application/msword" reset="true">
<cfoutput>#fileContent#</cfoutput>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    相关资源
    最近更新 更多