ASP.NET HttpHandler.
Opens files as application instead of in the browser window
// Attachment.cs // ASP.NET HttpHandler. Files types designated to be handled by this // HttpHandler are served to the user-agent as an "attachment" by adding // the "Content-Disposition" header to the HTTP response headers. // ========================================================================== // DESCRIPTION: // When browsing the Web, documents such as PDF, Excel, Word and others are // typically opened by the user-agent (UA) using the associated application, // but embedded within the browser window. The application's UI often appears // significantly different when so embedded causing difficulty for the user. // Also, users are accustomed to clicking the application's close box when // finished working which has the negative effect of also closing the UA. // // The solution is to offer users the choice to open non-Web documents in // the native application or save them to disk. The "Content-Disposition" // header defined by [RFC 1806] allows such a solution. // // This HttpHandler adds the following header to the HTTP response: // Content-Disposition: attachment; filename=file // // This header advises the UA that the file should be treated as an // attachment instead of being rendered inline.
- Download ContentDisposition.zip (5 kB)
INSTALL THE HTTP HANDLER ASSEMBLY:
Place the assembly that contains the handler in your application's \bin subdirectory (or, you can install it into the server's Global Assembly Cache.)
References:
Troost, R. and Dorner, S., Communicating Presentation Information in Internet Messages: The Content-Disposition Header, RFC 1806, June 1995. Neilsen, J., Open New Windows for PDF and other Non-Web Documents, Jakob Nielsen's Alertbox, August 29, 2005.Further Information
Regarding: http://www.unrealtower.org/pdfdownload I put together a Windows/IIS 6 solution: CREATE AN HTTP HANDLER CLASS (in C#) TO DO THE DIRTY WORK:namespace Tek4.HttpHandler.pdf { /// HttpHandler for PDF files. Causes files to be opened as an /// application instead of in the web browser window. /// This is accomplished by adding the follow HTTP header: /// Content-Disposition: attachment; filename=file.pdf /// [RFC1806] public class pdf : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext c) { // physical path of requested PDF file string physicalPath = c.Request.PhysicalPath; // name of PDF file string file = (new System.IO.FileInfo(physicalPath)).Name; // set content type for file // (should already be set) // c.Response.ContentType = "application/pdf"; // add Http header that causes file to open as application c.Response.AppendHeader("Content-Disposition", "attachment; filename=" + file); // write the file to the output stream c.Response.WriteFile(physicalPath); } } }
- INSTALL THE HTTP HANDLER ASSEMBLY: Place the assembly that contains the handler in your application's \bin subdirectory (or, you can install it into the server's Global Assembly Cache.)
- ADD THE HANDLER TO WEB.CONFIG FILE:
Add the handler to the processing pipeline by adding an <httpHandlers> section to your application's web.config file:
<system.web> <httpHandlers> <add path="*.doc" verb="*" type="Tek4.Web.HttpHandler.ContentDisposition.Attachment, Tek4.Web.HttpHandler.ContentDisposition, Version=1.0.2.0, Culture=neutral, PublicKeyToken=26c804c56bafc725" /> <add path="*.pdf" verb="*" type="Tek4.Web.HttpHandler.ContentDisposition.Attachment, Tek4.Web.HttpHandler.ContentDisposition, Version=1.0.2.0, Culture=neutral, PublicKeyToken=26c804c56bafc725" /> <add path="*.xls" verb="*" type="Tek4.Web.HttpHandler.ContentDisposition.Attachment, Tek4.Web.HttpHandler.ContentDisposition, Version=1.0.2.0, Culture=neutral, PublicKeyToken=26c804c56bafc725" /> </httpHandlers> </system.web>
- MAP THE PDF FILE EXTENSION TO BE HANDLED BY ASP.NET dll: Open the IIS management console. Select "Properties" for your web site. Click on the "Home Directory" tab. Click the "Configuration..." button. On the "Mappings" tab, click Add. Create a mapping for the "pdf" file extension that maps to the aspnet_isapi.dll executable (same executable as "aspx" extension). You can limit verbs to "GET". Check both "Script engine" and "Check that file exists". You can refer to this image to help clarify the above: http://www.eggheadcafe.com/articles/20030113.jpg
MSDN - IIS and ASP.NET Processing (Implementing HTTP Handler)




