c# - jQuery 对话框在 Response.TransmitFile 之后不会从代码隐藏中关闭

标签 c# jquery asp.net dialog download

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

ASPX 页面:

<%@ 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);
}

最佳答案

我自己找到了解决方案。 问题是 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 来执行下载。

关于c# - jQuery 对话框在 Response.TransmitFile 之后不会从代码隐藏中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256209/

相关文章:

c# - Asp.net C# 用 "a href"链接替换字符串的所有 url

c# - 在生产环境中调试应用程序

c# - 对基类库和框架类库的困惑

c# - Visual Studio 2010 : How to generate documentation out of code comments?

c# - asp :label control can hold? 文本有多少限制

jquery - 如何绑定(bind)div的点击事件

jquery - 我可以在单个 html 页面中多次使用 jquery 的 $(document).ready() 吗?

jQuery 应用程序问题。这是一个小故障还是不正确的编码?

c# - 通过 linq 数据源修改 asp.net 网格

c# - 在 ASP.NET/C# 中将文本附加到标签?