Search code examples
ajaxpdftelerikresponseradgrid

Download Pdf with ImageButton on Radgrid error with ajax


I am trying to download a pdf that is stored in my db table.The code works fine if I don't have ajax. On the web browser when I do inspect I get the error below.

 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'.

When I remove the ajax is working fine!

  <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>

The ImageButton (is on 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>

My code Behind

  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();




            }
        }

What I am doing wrong and how to fix it or if i cant download it how I can view it only in a new tab?


Solution

  • I manage to fix the error with ajax with the code below

          <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>