【问题标题】:Generate multiple files, add them to a zip and then send the zip as a file download.生成多个文件,将它们添加到 zip,然后将 zip 作为文件下载发送。
【发布时间】:2014-01-27 12:14:03
【问题描述】:

我有一个生成多个电子表格对象的 cfm 页面,现在如果我尝试发送文件以供下载,只有最后生成的电子表格文件会作为下载文件发送。

有没有办法将这些文件写入 zip 并将 zip 文件作为下载文件发送?

这是我现有的逻辑:

for(VARIABLES.fileNumber = 0; VARIABLES.fileNumber  < VARIABLES.maxFiles; VARIABLES.fileNumber = VARIABLES.fileNumber + 1)
{
   /* code to create Spreadsheet here */




    VARIABLES.context = getPageContext();
    VARIABLES.context.setFlushOutput(true); 

    VARIABLES.response = VARIABLES.context.getResponse().getResponse();
    VARIABLES.response.reset();
    VARIABLES.response.setContentType("application/msexcel");   
    VARIABLES.response.setContentLength(len(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj)));       
    VARIABLES.response.setHeader("Content-Disposition","attachment;filename=MyComplianceStatusFile#VARIABLES.fileNumber#Of#VARIABLES.maxFiles#.xls");

    VARIABLES.out = response.getOutputStream();     
    VARIABLES.out.write(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj));       


    VARIABLES.out.flush();      
    VARIABLES.out.close();  
  }

现在,这样我只能得到最后一个生成的电子表格。有没有办法让所有生成的电子表格一个一个或者一个 zip 文件?

【问题讨论】:

  • 顺便说一句,下载单个文件的方法在 .jsp 中更为典型。 CF 中的标准(更简单)方法是使用cfcontent。如果您是 CF 新手,我建议您先查看 tags by functionfunctions by category 视图,了解此类问题。

标签: coldfusion coldfusion-9 coldfusion-8 coldfusion-10


【解决方案1】:

是的,有:&lt;cfzip&gt;。文档中有一个用法示例,但我会在这里复制,所以我不会因为基本上说“RTFM”而受到打击:

<!--- This example shows how to zip the directory "c:\temp" into the ZIP file "e:\work\abc.zip". ---> 
<cfzip file="e:\work\abc.zip" source="c:\temp"> 

<!--- This example shows how to zip all the class files in a directory and add a subdirectory named "classes" to the JAR file entry name. ---> 
<cfzip file="e:\work\util.jar" action="zip" source="c:\src\util\" prefix="classes" filter="*.class"> 

<!---This example shows how to zip all of the log files in the ColdFusion directory and create a subdirectory called exception where zipped files are archived. 
<cfzip file="c:\zipTest\log2.zip" action="zip" source="c:\ColdFusion\" prefix="exception" filter="*.log"> 

<!--- This example shows how to overwrite all of the content of a ZIP file with the entries specified in the source. ---> 
<cfzip file="c:\currentApp.zip" source="c:\myApp\work" overwrite="yes">

【讨论】:

  • 你又去喂人了。
  • 我知道,对不起。我已经厌倦了“哦,FFS,JFGI”。
  • 我认为在任何人开始编写单行 ColdFusion 之前,必须先将他们介绍给文档。或者更好的是,谷歌。
  • @AdamCameron,几年后又回来了!抱歉这个愚蠢的问题,那时我对软件开发还很陌生。
猜你喜欢
  • 1970-01-01
  • 2019-03-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多