我正在使用CFcontent將電子表格從服務器傳輸到我的瀏覽器。我看到我的新手眼睛看起來很不正常。流式傳輸正在發生,但流式傳輸中的代碼沒有得到執行,我無法弄清楚原因。cfcontent調用順序錯綜複雜
首先,我設置了一個隱藏的輸入(默認爲「0」,當用戶點擊「下載」按鈕時會切換到「1」)。
<cfparam name="txtDoDownload" default="0">
<cfif IsDefined("form.txtDoDownload")>
<cfset txtDoDownload = form.txtDoDownload>
</cfif>
<cfinput type="hidden" name="txtDoDownload" id="txtDoDownload" value="#txtDoDownload#">
然後,我跑了 「下載」 的代碼,如果隱藏的輸入Form.txtDiDownload爲1
<cfoutput>
<cfif IsDefined("Form.txtDoDownload")>
<!--- THE FOUR ALERTS BELOW DON'T EXECUTE AFTER THE BUTTON IS CLICKED --->
<!--- BUT THE DOWNLOAD IS STILL HAPPENING --->
<script>alert("Foo!")</script>
<script>alert("Form.txtDoDownload: " + #Form.txtDoDownload#)</script>
<cfif "#Form.txtDoDownload#" EQ 1>
<script>alert("Downloading . . . ")</script>
<cfif IsDefined("Alums.Recordcount")>
<script>alert("In xlDownload")</script>
<cfset sObj = SpreadsheetNew("AlumniList","true")>
<cfscript>
SpreadsheetAddRows(sObj,Alums);
</cfscript>
<cfspreadsheet action="write" filename="C:\DevRoot\test.xlsx" name="sObj" overwrite="true">
<cfheader name="Content-Disposition" value="inline; filename=temp.xlsx">
<cfcontent deletefile="yes" type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" file="C:\wwwroot\test.xlsx">
</cfif>
<script>resetDownload();</script>
</cfif>
</cfif>
</cfoutput>
這裏的按鈕:
<cfinput type="button" value="Download to Excel" id="btnDLXL" name="btnDLXL" onClick="initDownload()">
這裏是那個時候執行的JavaScript該按鈕被點擊:
function initDownload() {
$('#txtDoDownload').val(1);
alert("initDownload (txtDoDownload): " + $('#txtDoDownload').val());
$('#AlumForm').submit();
}
下載發生得很好,但點擊按鈕後沒有觸發下載的警報(在第一次點擊按鈕之前觸發了幾個觸發)。這是一個「執行問題的順序?」有任何想法嗎?謝謝!
我能想到的一件事是:在提交時,CF只能返回一個頁面,或者流式傳輸一個文件,但是不能在同一個提交中執行這兩個操作? – SlimJim