Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have to download a pdf file on a link button (which is inside update panel) clickable from a content page.

I am using following code :

C#
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition","attachment;filename=Softgen.pdf");
Response.TransmitFile(Server.MapPath("Hosts_Win_Brochure.pdf"));
Response.End();


but it is giving error:

Microsoft JScript runtime error: 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
%��
4 0 ob'.
Posted
Updated 2-Nov-10 22:52pm
v3

1 solution

Hi Naveen,

You have to define ControlID={You linkbutton ID} in <triggers>
XML
<asp:UpdatePanel runat="server" ID="upFiles" UpdateMode="Conditional">
                               <ContentTemplate>
                               </ContentTemplate>
                               <Triggers>
                                   <asp:PostBackTrigger ControlID="gvNewFiles" />
                               </Triggers>
                           </asp:UpdatePanel>


Please use following code to Download file:
VB
Public Shared Function DownloadFile(ByVal filePath As String) As Boolean
        Try
            If File.Exists(HttpContext.Current.Server.MapPath(filePath)) Then
                Dim strFileName As String = ""
                strFileName = System.IO.Path.GetFileName(HttpContext.Current.Server.MapPath(filePath))
                strFileName = strFileName.Replace(" ", "%20")
                HttpContext.Current.Response.ContentType = "application/octet-stream"
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName) ' System.IO.Path.GetFileName(HttpContext.Current.Server.MapPath(filePath)))
                HttpContext.Current.Response.Clear()
                HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath(filePath))
                HttpContext.Current.Response.End()
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function



Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
Rohit from Delhi 3-Nov-10 3:40am    
i used the below lines. but it is giving same error.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<contenttemplate>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>

<Triggers>
<asp:PostBackTrigger ControlID="LinkButton1" />
</Triggers>


Link button is in contentplaceholder1 which is in update panel
</asp:UpdatePanel>
Dalek Dave 3-Nov-10 4:51am    
Good Call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900