Asp:从ashx文件下载文件时,进度不会结束

时间:2014-02-20 10:48:20

标签: asp.net updatepanel updateprogress

我尝试使用asp:progress所以当我点击我的asp:按钮时,它会调用一个写入CSV响应的ashx文件。

到目前为止,当我点击按钮时,动画显示正确,下载开始。但是,当文件开始下载时(当我从ashx文件中获得响应时),我似乎无法停止动画。

这是aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="MainPlaceHolder" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <p>Download a CSV file</p>
            <asp:Button ID="Button2" runat="server" Text="Download" 
            CausesValidation="False" onclick="Button2_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:UpdateProgress ID="UpdateProgress1"  AssociatedUpdatePanelID="UpdatePanel1" runat="server">
        <ProgressTemplate>
            <div id="AlertDiv" style="background:White">
                <asp:Image ID="imgLoading" runat="server" ImageUrl="css/smoothness/images/animated-overlay.gif" Width="34px" />Loading...
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>
</asp:Content>

这是Button2_click函数:

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        Response.Redirect("ProductsHierarchyDownload.ashx");
    }
    catch (Exception ex)
    {
      //log
    }
}

这是ashx文件中的ProcessRequest函数:

public void ProcessRequest(HttpContext context)
{
    string attachment = String.Format("attachment; filename=Hierarchie des produits au {0}.csv", DateTime.Today.ToShortDateString());
    context.Response.AddHeader("content-disposition", attachment);
    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("Pragma", "public");
    context.Response.ContentEncoding = Encoding.GetEncoding(1252);
    context.Response.Write(DemandeTarifImageBLL.DataTableToCSVString());
}

我尝试使用javascript和PageRequestManager类的属性endRequest,beginRequest和initializeRequest事件(我找到了一些conde片段here)但到目前为止我没有任何工作。

如何在下载开始时强制动画停止?

3 个答案:

答案 0 :(得分:1)

虽然我无法尝试看看它的工作情况,但根据我之前的经验,我可以提出以下建议。尝试更改代码,如下所示:

public void ProcessRequest(HttpContext context)
{
    string attachment = String.Format("attachment; filename=Hierarchie des produits au {0}.csv",        
    DateTime.Today.ToShortDateString());
    context.Response.AddHeader("content-disposition", attachment);
    context.Response.ContentType = "text/csv";
    context.Response.AddHeader("Pragma", "public");
    context.Response.ContentEncoding = Encoding.GetEncoding(1252);
    context.Response.Write(DemandeTarifImageBLL.DataTableToCSVString());
    context.Response.Flush();
    context.Response.End();
}

添加Flush和End清除缓冲区并分别标记响应结束。通过这种方式,客户可以得出请求已经完全处理的明确结论。

您还可以使用客户端脚本控制更新进度,以下是示例代码:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequest);

function EndRequest(sender, args) {
            if (postBackElement.id == 'Panel1Trigger') {
                $get('UpdateProgress1').style.display = 'none';
            }
        }

上面的js代码为更新面板结束请求事件添加了一个处理程序,并在该函数上关闭了Update进程。

尝试将handeler的ProcessRequest函数的内容放到按钮单击事件中,并记得为响应添加Flush()和End函数。

编辑1:

在进一步查看堆栈溢出时,我发现以下链接与您的问题类似,这证实了我的假设您无法在更新面板中进行重定向:

asp:UpdateProgress never goes away after clicking on downloadable content inside a repeater

希望这有帮助。

答案 1 :(得分:1)

尝试从javascript重定向到ashx。

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "downloadFileExport", "javascript:window.location ='" + ResolveUrl(downloadUrl) + "';", true)

这也解决了我的问题。

答案 2 :(得分:0)

在我的ASP.Net Web窗体应用程序中,即使在完成该过程后,仍会显示进度文本。通过增加ScriptManager中的超时,我能够在Dev环境中修复它。示例如下。

enter image description here