Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have to download a pdf file on a link button click from a content page.
i am using following code :
MIDL
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.<br />
Details: Error parsing near '%PDF-1.5<br />
%��<br />
4 0 ob'
.

Same code is working with normal page.
Posted
Updated 1-Nov-10 19:26pm
v2
Comments
NMehta83 2-Nov-10 1:44am    
Can you tell me where you put your button? means inside the update panel or outside? Can you please copy your aspx page code here in the question?

1 solution

Hi Naveen,

you can do using following function with passing file path
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
 
Comments
Rohit from Delhi 2-Nov-10 1:25am    
This code also giving the same error.
Sunasara Imdadhusen 2-Nov-10 3:11am    
The link button is inside the Update Panel?
Rohit from Delhi 3-Nov-10 3:24am    
yes it is inside update panel.

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