微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 CFM 页面下载为 Word Doc

如何解决将 CFM 页面下载为 Word Doc

我正在尝试使用 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 声明可能仅在输入的最开始时出现”。我用 Notepad++ 检查了下载的文档,并在顶部看到了这个:


<?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(基本上切断 <html 之前的所有内容),该文档将在 Word 中打开。是否可以去掉 xml 和 DOCTYPE 标签并使用 <htmlcfinclude 开始?或者,我应该采取其他方法吗?

解决方法

使用您的技术,您可以做的是使用 PageToDownload.cfm<cfsavecontent> 的内容放入变量中。一旦内容位于所述变量中,您就可以去掉开始 <html> 标记之前的所有内容,然后在 <cfoutput><cfheader> 语句之后去掉 <cfcontent> 该变量。

您的代码可能如下所示。

<!--- 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。