Monday, February 8, 2010

IIS - Forcing website PDFs to download as a file, not embed in the web page

I can’t take any credit for this, another member of my team came up with this nice solution to make a website download a PDF (or any file, we just needed to do this to a PDF…) as a file instead of opening it embedded in the browser.  This is a change applied in IIS (on IIS 6 – not tried it in Windows 2008 yet) to either a directory or a single file.  Watch out you don’t apply it to a directory with ASP or HTML file in it!

Right click the directory or file in IIS Manager, click Properties.  Click HTTP Headers and Add.  Use the name “content-disposition” and value “attachment”…

image

For added compatibility with different browsers (Firefox likes this) click MIME types, select New and enter the extension (.pdf in this case) and MIME type “application/x-download”…

image

Click OK…

image

And OK to finish.  The clients might have to clear their temporary internet files to get the full effect, but its tested and working in Firefox 3.6 and IE8.  A bit cleaner than the previous solution we were using of an ASP page processing each PDF download in memory…

No comments:

Post a Comment