2

我需要通过向他发送存储在数据库中的二进制文件并更新数据库来处理用户的按钮单击操作,并且使用 ajax 反映在 RadGrid 中的更改。

在执行操作、发送文件和更新网格时,它分别发生得很好,但是当尝试同时执行这两个操作时,它会返回一个错误,指出无法处理响应。

我想知道是否有任何替代方法可以做到这一点。

我有类似的代码:

javascript:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
         <script type="text/javascript">
             var pbControl = null;
             var prm = Sys.WebForms.PageRequestManager.getInstance();
             prm.add_beginRequest(BeginRequestHandler);

             Sys.WebForms.PageRequestManager.getInstance()
                                        .add_endRequest(EndRequestHandler);

             function BeginRequestHandler(sender, args) {
                 $(".loads").attr("disabled", true);
             }

             function EndRequestHandler(sender, args) {
                 if (args.get_error() != undefined) {
                     var errorMessage = args.get_error().message;
                         args.set_errorHandled(true);
                         errorMessage = errorMessage.replace(
                          "Sys.WebForms.PageRequestManagerServerErrorException:", 
                          "");
                         alert(errorMessage);
                }
                 $(".loads").attr("disabled", false);
              }
        </script>
</asp:ScriptManager>

ASP.NET:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div class="loads">
            <telerik:RadGrid ID="Grid" runat="server" 
                    OnItemCommand="Grid_ItemCommand">
                <MasterTableView TableLayout="Auto">
                    <Columns>
                        <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <asp:ImageButton ID="Button" runat="server" 
                                  CommandName="Command" 
                                  CommandArgument='<%# Eval("Code") %>' />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

C#:

protected void Grid_ItemCommand(object sender, 
                                Telerik.Web.UI.GridCommandEventArgs e)
{
    if(e.CommandName == "Command")
    {
        // Update database
        DatabaseUpdate();
        Grid.Rebind();

        // Send binary
        HttpResponse response = this.Response;
        byte[] bin = GetBinary(code);

        MemoryStream memStream = null;
        memStream = new MemoryStream(bin);

        response.Clear();
        response.AddHeader("content-disposition", 
            string.Format("attachment; filename={0}", "file.pdf"));
        response.ContentType = string.Format("application/{0}", "pdf");
        response.BufferOutput = false;

        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            response.OutputStream.Write(buffer, 0, bytesRead);
        }

        response.Flush();
    }
}
4

1 回答 1

0

有时,来自服务器的多个响应为它们相互争斗提供了一种方式,主要是在您使用 HttpResponse 类时。不,问题你可以做一个把戏。添加一个用于推送文件的空白页面,例如download.aspx

在您的Grid_ItemCommand事件中,只需删除发送文件的代码。只做数据库更新。

 protected void Grid_ItemCommand(object sender, elerik.Web.UI.GridCommandEventArgs e)
 {
        if(e.CommandName == "Command")
        {
            // Update database
            DatabaseUpdate();
            Grid.Rebind();
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "download","pushFile();", true);
        }
 }

上面代码的最后一行调用了一个脚本pushFile in aspxpage 和下面的脚本来打开我们新添加的页面download.aspx

Javascript:

function pushFile()
{
  window.open("download.aspx");
}

然后最后在后面的代码中download.aspx添加下面的代码Page_Load

protected void Page_Load(object sender, EventArgs e)
{
        HttpResponse response = this.Response;
        byte[] bin = GetBinary(code);

        MemoryStream memStream = null;
        memStream = new MemoryStream(bin);

        response.Clear();
        response.AddHeader("content-disposition", 
            string.Format("attachment; filename={0}", "file.pdf"));
        response.ContentType = string.Format("application/{0}", "pdf");
        response.BufferOutput = false;

        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            response.OutputStream.Write(buffer, 0, bytesRead);
        }

        response.Flush();
}
于 2016-02-10T09:12:27.557 回答