Share via


Force PDF download using VB.NET

Question

Friday, May 20, 2011 10:17 AM

I am using Visual Studio 2010.  I have created several PDFs using Adobe Acrobat Standard 9.

I am trying to force the download of the selected PDF to the user's local machine.

I am using an asp.imagebutton to trigger the code behind that first updates the SQL database with the info about the download and then calls a Public sub: ForcePDF Download.

However when the code is run it returns a usless error that tells me there was an error.

Here is the Download code I am using:

   Public Sub ForcePDFDownload(ByVal Response As HttpResponse, ByVal FullVirtualPath As String, ByVal fileName As String)

        Response.ClearHeaders()
        Response.ClearContent()
        Response.ContentType = "application/octet-stream"
        Response.Charset = "UTF-8"
        Response.AddHeader("content-disposition", "attachment; filename=" & fileName)
        Response.WriteFile(FullVirtualPath) 
        Response.End()

    End Sub

It is important that the file be downloaded to the clients local machine rather that simply send the PDF to a browser window. I have tried substituting "application/pdf" for the ContentType with the same reasults. The filename is simply the PDF name, i.e. Book1.pdf

The FullVirtualPath is sent from the calling codebehind and is obtained using:
Server.MapPath("~/TargetDirectory/PDF/" & filename)

Can I please get some help?

 

 

All replies (6)

Monday, May 23, 2011 1:40 AM ✅Answered

Hi ArtHandy,

since the code from the link i gave you should work (i tested it), please create a new web application in your Visual Studio. Add a form and copy your pdf file in the upper most folder and use this code:

NOTE: please use Response.ContentType = "application/pdf";

html

<body>
    <form id="form1" runat="server">
        click button to download PDF file<br />
        <asp:Button ID="Button1" runat="server" Text="Download" 
            onclick="Button1_Click1" />
    </form>
</body>

C# code

protected void Button1_Click1(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=testing.pdf");
    Response.TransmitFile(Server.MapPath("~/testing.pdf"));
    Response.End();
}

VB code

        Response.ContentType = "application/pdf"
        Response.AppendHeader("Content-Disposition", "attachment; filename=testing.pdf")
        Response.TransmitFile(Server.MapPath("~/testing.pdf"))
        Response.End()

I think the problem lies in ur application or in your pdf file. To debug, i would need both.

 


Monday, May 23, 2011 9:07 AM ✅Answered

Thank you so much for your feedback. I created the web application as you suggested. It worked perfectly. Once I realized that the PDF was OK I knew it had to be my application. Feeling like an idiot, I realized I was trying to execute the download within an AJAX framework. Once I excluded the imagebutton/download from the AJAX framework the download worked just fine.

Thanks again to those who took the time to help me figure this out. If you would like to check out the results you can go to: http://bit.ly/jaCgYM

This issue is solved!

 


Friday, May 20, 2011 10:24 AM

Hi,

please use the code in this link: Downloading a File with a Save As Dialog in ASP.NET

and for the future Convert C# to VB.NET - A free code conversion tool


Friday, May 20, 2011 2:30 PM

Well I tried following the advice given and generated the following code:

 Response.ContentType = "application/octet-stream"        Response.AppendHeader("content-disposition", "attachment; filename=" & fileName)        Response.TransmitFile(FullVirtualPath)        Response.End()

but it still returns the following 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
%��
429 0 o'.


Sunday, May 22, 2011 3:23 PM

I think you'd want to use the correct content type of application/pdf plus the content-disposition.

However, I'd make sure that the file is actually getting sent to the client properly. You can use a tool like Fiddler, FireBug or the IE9 developer tools to check the actual data sent to the client. Check if there is other content in the output sent to the client in addition to the actual PDF data or whether the headers have been modified in some way that would pre-clude displaying the content.

It looks like in your case the latter is the case.

You might also want to try a different browser just to see if it's maybe some IE configuration issue - IE has a bunch of options on how to display content and sometimes ignores display related headers depending on the config settings.

+++ Rick


Sunday, May 22, 2011 6:11 PM

rstrahi, thanks for the feedback. All the PDFs are created using ADOBE Acrobat Standard 9 from MS Word documents. As I stated earlier I have already tried using the "application/pdf" with the same result. The results are the same wehter I use IE9, Firefox or Safari. The PDFS appear correct when displayed directly with a Browser regadless of which one. I can find no other output being sent to the client.

Any additional ideas?