使用 Radgrid 上的 ImageButton 下载 Pdf 时出现 ajax 错误

Download Pdf with ImageButton on Radgrid error with ajax

我正在尝试下载存储在我的数据库中的 pdf table.The 如果我没有 ajax,代码也可以正常工作。当我在网络浏览器上检查时,出现以下错误。

 Sys.WebForms.PageRequestManagerParserErrorException: 
 The message received from the server could not be parsed.
 Common causes for this error are when the response is modified by calls
 to Response.Write(), response filters, HttpModules, or server trace is  enabled.
 Details: Error parsing near '%PDF-1.5
 %�쏢
 11 0 ob'.

当我删除 ajax 时工作正常!

  <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest"  >
      <AjaxSettings>
             <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="CustomerGrid" UpdatePanelCssClass="" />
                     <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" UpdatePanelCssClass="" />
                 </UpdatedControls>
             </telerik:AjaxSetting>
             <telerik:AjaxSetting AjaxControlID="RadWindowManager1">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="CustomerGrid" UpdatePanelCssClass="" />
                 </UpdatedControls>
             </telerik:AjaxSetting>
          </AjaxSettings>
                </ClientEvents>
    </telerik:RadAjaxManager>

ImageButton(在 Radgrid 上)

<telerik:GridTemplateColumn UniqueName="Download" FilterControlAltText="Filter Delete column" AllowFiltering="false"  >
  <ItemTemplate>
  <asp:ImageButton ID="ImageButtonDownload" runat="server" AlternateText="Download" CommandName="Download" Width="35px"  
    ImageUrl="~/assets/images/pdf.jpg"  />
     </ItemTemplate>
   <HeaderStyle Width="13px" />
    </telerik:GridTemplateColumn>

我的代码隐藏

  protected void downloadFile(string fileID)
        {
            {
                byte[] file;
                string Title = "";

                try
                {


                    string download = "Select agreementpdf,pdfname From companie WHERE code = @ID";
                   MySqlCommand cmd = new MySqlCommand(download, con);
                   con.Open();

                    cmd.Parameters.AddWithValue("@ID", fileID);
                    MySqlDataReader  reader = cmd.ExecuteReader();
                    reader.Read();
                    file = (byte[])reader["agreementpdf"];
                    Title = reader["pdfname"].ToString();

                }

                finally
                {
                    con.Close();
                }

                //CREATE RESPONSE TO DOWNLOAD FILE
                Response.AddHeader("Content-disposition", "attachment; filename=" + Title);

                Response.ContentType = "application/pdf";

                Response.BinaryWrite(file);
                Response.Flush();
                Response.End();
                //HttpContext.Current.ApplicationInstance.CompleteRequest();




            }
        }

我做错了什么以及如何修复它,或者如果我无法下载它,我如何只能在新标签页中查看它?

我设法用下面的代码 ajax 修复了错误

      <telerik:RadScriptBlock ID="RadScriptBlock" runat="server">
           <script type="text/javascript">
 function onRequestStart(sender, args) {
                   if (args.get_eventTarget().indexOf("ImageButtonDownload") >= 0)
                   args.set_enableAjax(false);
              }
    </script>
        </telerik:RadScriptBlock>


           <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest" 
            ClientEvents-OnRequestStart="onRequestStart"  >
      <AjaxSettings>
             <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="CustomerGrid" UpdatePanelCssClass="" />
                     <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" UpdatePanelCssClass="" />
                 </UpdatedControls>
             </telerik:AjaxSetting>
             <telerik:AjaxSetting AjaxControlID="CustomerGrid">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="RadScriptBlock" UpdatePanelCssClass="" />
                     <telerik:AjaxUpdatedControl ControlID="CustomerGrid" UpdatePanelCssClass="" />
                 </UpdatedControls>
             </telerik:AjaxSetting>
             <telerik:AjaxSetting AjaxControlID="RadWindowManager1">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="CustomerGrid" />
                 </UpdatedControls>
             </telerik:AjaxSetting>
          </AjaxSettings>
               <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
    </telerik:RadAjaxManager>