0

I have this code..

<li><a href="downloads/PDF_File.pdf">PDF</a></li>

but it opens the pdf file, now I am new to ASP.NET, how do I get the download dialog box to open?

user979331
  • 11,039
  • 73
  • 223
  • 418
  • Check this: http://stackoverflow.com/questions/364946/how-to-make-pdf-file-downloadable-in-html-link – carny666 Jun 05 '12 at 14:54

2 Answers2

0

Basically, what is happening here is the normal behaviour for pdf files. IIS by default serves up the "pdf" MIME-type for any pdf files in your web application. When you access a pdf in your application, the browser reads the MIME-type and understands that you are accessing a pdf file.. most browsers will want to display it in their built-in PDF reader instead of prompting you to save it. If you really need a download dialog box to show up for pdfs, you could change the MIME-type for pdf in web.config so that IIS will serve up pdfs as a basic filetype within your application:

<configuration>
    <system.webServer>
        <staticContent>
            <remove fileExtension=".pdf" />
            <mimeMap fileExtension=".pdf" mimeType="application/octet-stream" />
        </staticContent>
    </system.webServer>
</configuration>

Note: You should first remove the MIME-type you are manually setting in web.config, because a MIME-type for the same extension may already be set at the application level.

Now IIS will serve up pdf files as a basic/unknown filetype and they will be downloadable. This works for any filetype if you just swap out '.pdf' for a different extension.

BumbleB2na
  • 10,723
  • 6
  • 28
  • 30
  • I tried that but got this error The requested page cannot be accessed because the related configuration data for the page is invalid. – user979331 Jun 05 '12 at 15:01
  • hey, i edited the answer a bit. Make sure that you put things in the right spot because it sounds like a web.config markup error. – BumbleB2na Jun 05 '12 at 15:04
  • tried that still getting the same error The requested page cannot be accessed because the related configuration data for the page is invalid. – user979331 Jun 05 '12 at 15:04
  • no it should work please check your web.config this article explains how to add mimetypes the same way that I just did: http://blogs.iis.net/bills/archive/2008/03/25/how-to-add-mime-types-with-iis7-web-config.aspx Also feel free to edit your question and post your entire web.config so that I can take a look.. – BumbleB2na Jun 05 '12 at 15:05
  • this what the config error looks like Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.pdf' – user979331 Jun 05 '12 at 15:10
  • you have pdf mimetype being set at the application level. You need to remove it in web.config prior to adding it in. Please see my updated answer. – BumbleB2na Jun 05 '12 at 19:59
0

I don't know how to do this with existing files, but a while ago I wrote a piece of code combining iTextSharp and an ASP.net (framework 4) MemoryStream object to create then download pdf files, I hope this could be helpful :

MemoryStream msPDF = new MemoryStream();
// do some stuff with iTextSharp ...
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=myPdf.pdf"); // open/save dialog
Response.BinaryWrite(msPDF.ToArray());
  • I had also to append the content length, to make it work, like so: `Response.AddHeader("Content-Length", "" + data.Length);` – Marcel Sep 06 '13 at 14:25