在Response.TransmitFile之后,jQuery对话框不会从代码中关闭

时间:2014-03-07 17:04:21

标签: c# jquery asp.net dialog download

我的问题是,当我从对话框后面的代码调用关闭对话框时使用引用的aspx页面不会关闭。 如果我评论响应传输文件部分代码,则对话框正确关闭,否则下载开始但对话框仍然打开。 如果您有任何建议,请告诉我,谢谢!

ASPX PAGE:

<%@ Page Async="true" AsyncTimeout="30" Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="CreateUI.ascx" tagname="CreateUI" tagprefix="uc1" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <%--JQuery--%>

    <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>

    <link href="Styles/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        $(document).ready(function () {
            $('#jobDone').dialog({
                autoOpen: false,
                draggable: true,
                title: "Job completed",
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }
            });
        });

        function showDialog(id) {
            $(function () {
                $('#' + id).dialog("open");
                return false;
            });
        }

        function closeDialog(id) {
            $(function () {
                $('#' + id).dialog("close");
                return false;
            });
        }
    </script>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <!-- ScriptManager to manage UpdatePanel -->
    <asp:ScriptManager ID="mainScriptManager" runat="server"></asp:ScriptManager>

    <!-- CreateUI Component -->
    <uc1:CreateUI ID="CreateUIForm" runat="server" />

    <!-- Hidden Field to pass data -->
    <asp:Table ID="TableMain" runat="server" CssClass="table">
        <asp:TableRow ID="TableRow1" runat="server">
            <asp:TableCell ID="TableCell1" runat="server">
                <asp:HiddenField ID="UI_Paths" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

    <!-- div linked to Jquery dialog -->
    <div id='jobDone'>
        <asp:UpdatePanel ID="UpdatePanelDownload" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
            <ContentTemplate>
                <asp:Label ID="LabelMessage" runat="server" Text="Operation ended successfully, do you want to download the produced files?</br></br>"></asp:Label>
                <asp:Button ID="ButtonDownload" runat="server" Text="Yes" Width="50px" onclick="ButtonDownload_Click" />
                <asp:Button ID="ButtonNo" runat="server" Text="No" Width="50px" OnClientClick="closeDialog('jobDone'); return false;" />
            </ContentTemplate>
        </asp:UpdatePanel>            
    </div>        

</asp:Content>

背后的代码:

private void DownloadFile(object uiPaths)
{
    UIGEN config = (UIGEN)System.Configuration.ConfigurationManager.GetSection("UIGENGroup/UIGEN");
    string toPath = config.sharedPath;
    if (!toPath.EndsWith(@"\"))
        toPath += @"\";

    string[] fileNamePaths = uiPaths.ToString().Split(new char[] { '*' });

    string zipName = toPath + DateTime.Now.ToString().Replace("/", "_").Replace(":", "_").Replace(" ", "_") + ".zip";
    SharpZipLib.CreateZip(zipName, null, fileNamePaths, SharpZipLib.FolderOffset.LastDirOnly);

    try
    {
        FileInfo file = new FileInfo(zipName);

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/x-zip-compressed";
        Response.Flush();
        Response.TransmitFile(file.FullName);
        Response.End();
    }
    catch (System.Exception ex)
    {
        //To do ...Manage the error
    }

    //Delete zip from Server Shared Folder
    if (File.Exists(zipName))
        File.Delete(zipName);
}

protected void ButtonDownload_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        DownloadFile(UI_Paths.Values);
    }

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), UniqueID, "closeDialog('jobDone');", true);
}

1 个答案:

答案 0 :(得分:3)

我自己找到了解决方案。 问题是Response.End();在执行ButtonDownload_Click中抵制的javascrip之前结束响应。

我尝试了建议的解决方案(在其他类似的线程中读取)将Response.End()更改为context.ApplicationInstance.CompleteRequest()但是下载不是那样开始的。

所以我删除了:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), UniqueID, "closeDialog('jobDone');", true);
来自ButtonDownload_Click的

我修改了aspx页面:

<asp:Button ID="ButtonDownload" runat="server" Text="Yes" Width="50px" onclick="ButtonDownload_Click" />

到此:

<asp:Button ID="ButtonDownload" runat="server" Text="Yes" Width="50px" OnClientClick="closeDialog('jobDone');" onclick="ButtonDownload_Click" />

这样ButtonDownload突然从javascript关闭对话框然后执行服务器端ButtonDownload_Click执行下载。