cfcontent呼叫令令人困惑

时间:2014-07-02 17:42:34

标签: javascript jquery coldfusion

我正在使用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();
}

下载很顺利,但点击按钮后没有触发下载周围的任何警报(在点击按钮之前,在第一次传递时会触发一些警报)。这是“执行顺序问题吗?”有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

@ user3738377在评论中是正确的。该请求是 - 在<cfheader><cfcontent>标记的指示下 - 使用test.xlsx进行回复,因此它不能回复上面的标记它。因此,标记永远不会被发送到浏览器,因此永远不会执行。

您需要将JS交互移动到上一个请求的末尾:按下按钮后,但在发出XLS文件请求之前。