2013-05-14 75 views
2

我有以下工作中途工作。我正在嘗試CFZip下載的Excel文件的內容,但涉及到一個問題。下面的代碼顯示有3張門票。 1張主票和2張票是主票的延長答案。所有門票,主要門票和兒童都可以使用附件。CFZip功能問題

當我嘗試下載Excel文件時,我也試圖下載兩個應該位於Zip文件內的文件夾。一個應該指向孩子,一個指向父母。兩者應有各自的附件。我在這裏錯過了一些大的代碼。

下面是代碼:

<cfquery datasource="#request.dsn#" name="mainTickets"> 
    SELECT s.ticketID,CAST(s.DateRaised as 
    varchar) AS DateRaised,s.Summary,s.RaisedBy,s.AssignedTo, 
    st.Status 
    ,CAST(s.LastUpdatedDate as varchar),CAST(s.TimeSpent as float) as 
    timespent,stt.TicketType,s.LastUpdatedComment 
    from supportTickets s 
    inner join SupportStatusType st on st.statusID = s.status 
    inner join supportTicketType stt on stt.TicketTypeID = s.TicketType 
    where s.ticketID IN (<cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ticketID#" list="yes"> 
    ) 
</cfquery> 
<cfset filename = "Detailed_Ticket_Summary" & dateformat(now(), 'mm_dd_yyyy') & "." & "xls"> 
<cfset s = spreadsheetNew("Tickets Summary")> 
<!--- Add header row ---> 
<cfset spreadsheetAddRow(s, "TicketID,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment, Details")> 
<!--- format header ---> 
<cfset spreadsheetFormatRow(s, #format1#, 1)> 
<cfset spreadsheetAddRows(s, mainTickets)> 
<cfset SpreadsheetFormatColumn(s, {textwrap=true}, 10)> 
<cfset SpreadsheetFormatColumn(s, {textwrap=true}, 3)> 
<cfloop from="1" to="#mainTickets.recordcount#" index="i"> 
    <cfset k = i + 1> 
    <cfset SpreadsheetSetCellFormula(s, 'HYPERLINK("[Tickets Summary]TicketID_#mainTickets.ticketID#!A1","Click Here")',#k#,11)> 
    <cfset SpreadsheetFormatCell(s, {bold=true, color='blue', bottomborder='dotted'}, #k#, 11)> 
</cfloop> 
<cfif mainTickets.recordcount> 
    <cfoutput query="mainTickets"> 
     <cfquery datasource="#request.dsn#" name="fetchTickets"> 
      SELECT s.ticketID,s.ticketNumber,CAST(s.DateRaised as 
      varchar),s.Summary,s.RaisedBy,s.AssignedTo, 
      st.Status 
      ,CAST(s.LastUpdatedDate as varchar),s.TimeSpent,stt.TicketType,s.LastUpdatedComment 
      from supportTicketsHistory s 
      inner join SupportStatusType st on st.statusID = s.status 
      inner join supportTicketType stt on stt.TicketTypeID = s.TicketType 
      where s.ticketnumber = <cfqueryparam cfsqltype="cf_sql_numeric" value="#mainTickets.ticketID#"> 
     </cfquery> 

     <!--- Add query ---> 
     <cfif fetchTickets.recordcount> 
      <cfset SpreadsheetCreateSheet(s, 'TicketID_#ticketID#')> 
      <cfset SpreadsheetSetActiveSheet(s, 'TicketID_#ticketID#')> 
      <!--- Add header row ---> 
      <cfset spreadsheetAddRow(s, "TicketID,Ticket Number,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment")> 
      <!--- format header ---> 
      <cfset spreadsheetFormatRow(s, #format1#, 1)> 
      <cfset SpreadsheetAddRows(s, fetchTickets)> 
      <cfset SpreadsheetFormatColumn(s, {textwrap=true}, 11)> 
      <cfset SpreadsheetSetActiveSheetNumber(s, 1)> 
      <cfset ielements = ValueList(mainTickets.ticketID)> 
      <cfquery datasource="#request.dsn#" name="getAttachments"> 
       SELECT Attach_FileName,Attach_FileSize,Attach_Time,SupportTicketID 
       FROM support_attachments 
       WHERE SupportTicketID = #val(fetchTickets.ticketnumber)# 
      </cfquery> 
      <cfquery datasource="#request.dsn#" name="getChkAttachments"> 
       SELECT Support_Attach_FileName,Support_Attach_FileSize,Support_Attach_Time, 
       Support_SupportTktHistoryID,Support_SupportTicketID 
       FROM support_attach_History 
       WHERE Support_SupportTicketID = #val(fetchTickets.ticketnumber)#AND Support_SupportTktHistoryID 
       in (SELECT ss.ticketID FROM supportTicketsHistory ss 
       WHERE ss.ticketNumber = #val(fetchTickets.ticketnumber)#) 
      </cfquery> 
      <!--- handle the attachments for the Ticket in ZIp Format ---> 
      <cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & getAttachments.SupportTicketID> 
      <cfif !DirectoryExists(currentDirectory)> 
       <cfdirectory action="create" directory="#currentDirectory#"> 
      </cfif> 

      <cfset currentDirectory2 = GetDirectoryFromPath(GetTemplatePath()) & getChkAttachments.Support_SupportTktHistoryID> 
      <cfif !DirectoryExists(currentDirectory2)> 
       <cfdirectory action="create" directory="#currentDirectory2#"> 
      </cfif> 
      <cfif getAttachments.recordcount> 
       <cfloop query="getAttachments"> 
        <cffile action="copy" 
         source="#ExpandPath('attachments/#getAttachments.Attach_FileName#')#" 
         destination="#currentDirectory#\"> 
       </cfloop> 
      </cfif> 

      <cfif getChkAttachments.recordcount> 
       <cfloop query="getChkAttachments"> 
        <cffile action="copy" 
         source="#ExpandPath('attachments/#getChkAttachments.Support_Attach_FileName#')#" 
         destination="#currentDirectory2#\"> 
       </cfloop> 
      </cfif> 
      <!--- handle the attachments for the Ticket in ZIP Format ---> 
      <cfset spreadsheetWrite(s, filename, true)> 
      <cfset dest2 = getTempDirectory() & "/" & "Tickets" & ".zip"> 
      <cfzip action="zip" file="#dest2#"> 
       <cfdirectory action="list" directory="#currentDirectory#/" name="listRoot"> 
       <cfdirectory action="list" directory="#currentDirectory2#/" name="listRoot2"> 
       <cfset lstFiles = ValueList(listRoot.name)> 
       <cfset lstFiles2 = ValueList(listRoot2.name)> 
       <cfloop list="#lstFiles#" index="k"> 
        <cfzipparam source="#currentDirectory#/#k#" recurse="yes" filter="*"/> 
       </cfloop> 
       <cfloop list="#lstFiles2#" index="kk"> 
        <cfzipparam source="#currentDirectory2#/#kk#" recurse="yes" filter="*"/> 
       </cfloop> 
      </cfzip> 
     </cfif> 
    </cfoutput> 
</cfif> 
<cfheader name="Content-Disposition" value="attachment;filename=#ListFirst(filename,'.')#.zip"> 
<cfcontent file="#dest2#" type="application/zip" deletefile="true" reset="true"> 
+2

你有很多的代碼有這無關壓縮和解了文件,因此它是什麼,你希望發生的,什麼是*實際*發生? – 2013-05-14 10:46:34

+0

代碼是相當大的,我試圖創建兩個文件夾與父母門票和兒童門票像ParentTicket(46)和ChildTicket(90)。現在用Excel工作表,我試圖下載兩個文件夾46,90,其中每個文件都與其中的文件相關聯在一起所有一個singkle zip文件應該包含兩個文件夾和一個excel表 – Misty 2013-05-14 11:47:06

+0

您聲明你的zip文件應該包含一個Excel文件和兩個文件夾。這些文件夾是空的嗎?我不認爲'cfzip'會存檔空文件夾。另外,如果要將文件夾路徑存儲在存檔中,則需要包含「storePath」屬性並將其設置爲「yes」。我不認爲我在你的代碼示例中看到了這一點。 – 2013-05-14 12:36:16

回答

1

我整理出來這裏是我做的方式。

<cfquery datasource="#request.dsn#" name="mainTickets"> 
     SELECT s.ticketID,CAST(s.DateRaised as 
     varchar) AS DateRaised,s.Summary,s.RaisedBy,s.AssignedTo, 
     st.Status 
     ,CAST(s.LastUpdatedDate as varchar),CAST(s.TimeSpent as float) as timespent,stt.TicketType,s.LastUpdatedComment 
     from supportTickets s 
     inner join SupportStatusType st on st.statusID = s.status 
     inner join supportTicketType stt on stt.TicketTypeID = s.TicketType 
     where s.ticketID IN (<cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ticketID#" list="yes">) 
    </cfquery> 
    <cfset filename = "Detailed_Ticket_Summary" & dateformat(now(),'mm_dd_yyyy') & "." & "xls"> 
    <cfset s = spreadsheetNew("Tickets Summary")> 
    <!--- Add header row ---> 
    <cfset spreadsheetAddRow(s, "TicketID,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment, Details")> 
    <!--- format header ---> 
    <cfset spreadsheetFormatRow(s,#format1#,1)> 
    <cfset spreadsheetAddRows(s, mainTickets)> 
    <cfset SpreadsheetFormatColumn(s,{textwrap=true},10)> 
    <cfset SpreadsheetFormatColumn(s,{textwrap=true},3)> 
    <cfloop from="1" to="#mainTickets.recordcount#" index="i"> 
    <cfset k = i+1> 
    <cfset SpreadsheetSetCellFormula(s, 'HYPERLINK("[Tickets Summary]TicketID_#mainTickets.ticketID#!A1","Click Here")',#k#,11)> 
    <cfset SpreadsheetFormatCell(s,{bold=true,color='blue',bottomborder='dotted'},#k#,11)> 
    </cfloop> 
    <cfif mainTickets.recordcount> 
    <cfoutput query="mainTickets"> 
     <cfquery datasource="#request.dsn#" name="fetchTickets"> 
      SELECT s.ticketID,s.ticketNumber,CAST(s.DateRaised as 
      varchar),s.Summary,s.RaisedBy,s.AssignedTo, 
      st.Status 
      ,CAST(s.LastUpdatedDate as varchar),s.TimeSpent,stt.TicketType,s.LastUpdatedComment 
      from supportTicketsHistory s 
      inner join SupportStatusType st on st.statusID = s.status 
      inner join supportTicketType stt on stt.TicketTypeID = s.TicketType 
      where s.ticketnumber = <cfqueryparam cfsqltype="cf_sql_numeric" value="#mainTickets.ticketID#"> 
     </cfquery> 

     <!--- Add query ---> 
     <cfset Temp = GetDirectoryFromPath(GetTemplatePath()) & 'temp'> 
     <cfif !DirectoryExists(Temp)> 
      <cfdirectory action="create" directory="#Temp#"> 
     </cfif> 
     <cfif fetchTickets.recordcount> 
      <cfset SpreadsheetCreateSheet(s , 'TicketID_#ticketID#')> 
      <cfset SpreadsheetSetActiveSheet(s , 'TicketID_#ticketID#')> 
      <!--- Add header row ---> 
      <cfset spreadsheetAddRow(s, "TicketID,Ticket Number,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment")> 
      <!--- format header ---> 
      <cfset spreadsheetFormatRow(s,#format1#,1)> 
      <cfset SpreadsheetAddRows(s , fetchTickets)> 
      <cfset SpreadsheetFormatColumn(s,{textwrap=true},11)> 
      <cfset SpreadsheetSetActiveSheetNumber(s, 1)> 
      <cfset ielements = ValueList(mainTickets.ticketID)> 
      <cfquery datasource="#request.dsn#" name="getAttachments"> 
      SELECT Attach_FileName,Attach_FileSize,Attach_Time,SupportTicketID 
      FROM support_attachments 
      WHERE SupportTicketID = #val(fetchTickets.ticketnumber)# 
      </cfquery> 
      <cfquery datasource="#request.dsn#" name="getChkAttachments"> 
      SELECT Support_Attach_FileName,Support_Attach_FileSize,Support_Attach_Time, 
      Support_SupportTktHistoryID,Support_SupportTicketID 
      FROM support_attach_History 
      WHERE Support_SupportTicketID = #val(fetchTickets.ticketnumber)# 
      AND Support_SupportTktHistoryID in (SELECT ss.ticketID FROM supportTicketsHistory ss 
      WHERE ss.ticketNumber = #val(fetchTickets.ticketnumber)#) 
      </cfquery> 
      <cfif getAttachments.recordcount> 
      <!--- handle the attachments for the Ticket in ZIp Format ---> 
      <cfset currentDirectory = "MainTicketID_" & "Number_" & getAttachments.SupportTicketID & "_Attachments"> 
      <cfdump var="#temp#\#currentDirectory#"> 
      <cfif !DirectoryExists("#temp#\#currentDirectory#")> 
       <cfdirectory action="create" directory="#temp#\#currentDirectory#"> 
      </cfif> 
      <cfdump var="#currentDirectory#"> 
       <cfif getAttachments.recordcount> 
       <cfloop query="getAttachments"> 
        <cffile action="copy" source="#ExpandPath('attachments/#getAttachments.Attach_FileName#')#" 
        destination="#temp#\#currentDirectory#\"> 
       </cfloop> 
       </cfif> 
      </cfif> 
     <cfif getChkAttachments.recordcount> 
      <cfset currentDirectory2 = "MainTicket_" & getChkAttachments.Support_SupportTicketID & "_Updated_Child_" & getChkAttachments.Support_SupportTktHistoryID & "_Attachments"> 
      <cfif !DirectoryExists("#temp#\#currentDirectory2#")> 
       <cfdirectory action="create" directory="#temp#\#currentDirectory2#"> 
      </cfif> 
       <cfif getChkAttachments.recordcount>  
       <cfloop query="getChkAttachments"> 
        <cffile action="copy" source="#ExpandPath('attachments/#getChkAttachments.Support_Attach_FileName#')#" 
        destination="#temp#\#currentDirectory2#\"> 
       </cfloop> 
       </cfif> 
     </cfif> 
     <!--- handle the attachments for the Ticket in ZIP Format ---> 
     <cfset spreadsheetWrite(s, filename, true)> 
      <cfset dest2 = getTempDirectory() & "/" & "Tickets" & ".zip"> 
      <cfzip action="zip" file="#dest2#" overwrite="true"> 
      <cfzipparam source="#temp#"/> 
      <cfzipparam source="#filename#"/> 
      </cfzip> 
     </cfif> 
     </cfoutput> 
     </cfif> 
    <cfdirectory action="delete" directory="#temp#" recurse="yes"> 
    <cfheader name="Content-Disposition" value="attachment;filename=#ListFirst(filename,'.')#.zip"> 
    <cfcontent file="#dest2#" type="application/zip" deletefile="true" reset="true"> 

這可以幫助別人,而不是如何嵌套的cfzip功能