coldfusion - cffile 在存储表时创建空行

标签 coldfusion cffile

我有一个保存表格的 cfsavecontent 标签。后来我用cffile把保存的内容写到一个文件中。当我查看该文件时,我发现在 <td> 之后插入了许多空行表格中的标签;在 </tr> 之后插入了几个空行标签。 (尽管它不会在一行代码中显示 <tr><td>&nbsp;</td></tr> 的地方执行此操作。)

目前我有一个包含其中两个表的文件。这些表在循环中生成,输出文件是使用 cffile append 创建的。这个文件有 915 行,其中可能有 30 行是非空白的。我所有的后续代码都可以正常工作,但这只是测试数据。在现实世界中,我可能有 1000 个或更多表,我担心文件大小。

代码:

<cfset head1 = 'from = "moxware" '>
<cfset head2 = 'to = "#hrep.PersonEmail#" '>   
<cfset head3 = 'replyto = "#replyto#" '>
<cfset head4 = 'subject = "#subject#" '>
<cfset head5 = 'type = "html" '>      

<cfsavecontent variable = "abc">
  <cfoutput>
   #head1#
   #head2#
   #head3#
   #head4#
   #head5# >
    #xyz#
  </cfoutput>
</cfsavecontent>

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "<cfmail"
    mode = "777" >

<cffile action = "append"
       file   = "/var/www/reports/moxrep/#reportout#.cfm"
       output = "#abc#"
       mode   = "777"> 

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "</cfmail>"
    mode = "777" >

关于 xyz,我正在从文件中读取它:

      <cffile action = "read"
      file   = "/var/www/reports/moxrep/#reportname#.cfm"
      variable = "xyz">

文件看起来像这样:

   <link rel="stylesheet" href="sample.css">
  <link rel="stylesheet" type = "text/css" href ="betty.css"/>
  <p style="margin-left:40px"><span style="font-size:14px"><span style="font- family:georgia,serif">Dear Customer,</span></span></p>

We were so pleased that you have signed up for one of our programs.&nbsp; Apparently you live in the city of {{1.&nbsp; Additionally we observe that your were referred to us by {{2.&nbsp; Below please find a listing of what you signed up for.</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-    family:georgia,serif">{{r</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Sincerely Yours,</span></span></p>

<p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">John Jones<br />
President<br />
XYZ Corporation</span></span></p>

该文件是由代码生成器创建的,而不是我创建的,所以有点麻烦。在代码的后面,我替换了以 {{ ; 开头的所有内容特别是 {{r 被替换为表格,这就是额外空间的来源。

append 本身并没有插入任何额外的行。

有谁知道是什么导致文件中出现这些额外的空行?以及如何摆脱它们?

最佳答案

Betty,如果您想避免出现空格,通常需要小心执行此操作。特别是将 cfoutput 与查询一起使用将生成行。所以这段代码:

<table>
<cfoutput query="myquery">
  <tr><td>#col1#</td><td>#col2#</td></tr>

</cfoutput>
</table>

会产生额外的行......但是如果你这样做:

<cfsetting enablecfoutputonly="yes">

<cfoutput><table></cfoutput>
<cfloop query="myquery"><cfoutput><tr><td>#col1#</td><td>#col2#</td></tr></cfoutput></cfloop>
<cfoutput></table></cfoutput>

您需要仔细控制允许添加到缓冲区的内容。 enableoutputonly 完全按照它说的去做......它不允许任何东西“进入缓冲区”,除非它包含在 cfoutputs 中。

希望这对您有所帮助。正如 cameron 所说,您应该针对此类问题粘贴代码。这就是答案通常所在的位置。

(您可能还需要试验 cffile 的“addnewline”属性 - 取决于您的问题是否是文件末尾的一行)。


要回答有关添加 cfsetting 的问题...在您的情况下,您正在将 CF 代码写入一个文件,然后稍后执行(顺便说一下,这通常不是一个好主意:)。因此,在您的第一个 Append 语句中:

<cffile action = "append"
file = "/var/www/reports/moxrep/#reportout#.cfm"
output = "<cfmail"
mode = "777" >

将“输出”更改为:

<cffile action = "append"
    file = "/var/www/reports/moxrep/#reportout#.cfm"
    output = "<cfsetting enablecfoutputonly=""yes""/> <cfmail"
    mode = "777" >

但是 Betty - 您仍然需要从 cfsavecontent 中删除换行符(如果这是您的空格的来源),因为它们实际上位于 cfoutput 中。此外,您创建要插入的表的代码可能有问题 - 此处未列出。

最后,由于这是 cfmail,请看一下这篇关于换行符的帖子,它可能有也可能没有一些意义——但至少给你多了一条信息:)

http://www.coldfusionmuse.com/index.cfm/2006/4/12/cfmail.linebreak

关于coldfusion - cffile 在存储表时创建空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292959/

相关文章:

coldfusion - cffile 正在覆盖现有文件

Coldfusion Amazon S3 支持文件上传,它是否连接到特定 IP?

mysql - 执行数据库查询时出错

asp.net-mvc - MVC : I need to understand the Model

coldfusion - CFFILE - 使用组件上传文件

file-upload - cfc 文件中的 Coldfusion FileUpload 函数不起作用

eclipse - 适用于 Coldfusion 的 Eclipse 的正确版本是什么?

sql-server - 如何在 SQL Server 中为整个查询设置排序规则以进行不区分大小写的搜索?

coldfusion - 从coldfusion中的自动下载网址获取文件

csv - 使用 cffile 循环 csv 时出现奇怪的字符