HttpHandler for Excel file download

When providing html links to XML files, typically the HTTP Headers will set ContentType = Plain / Text, and show up in the browser as text. The need is to provide links to Excel files in XML format, and prompt the user to download locally. Once downloaded, the user may open the XML file in Excel.

An alternate solution is to set the MIME type for files of XML extension, and open directly as a link from the browser yet rendered in Excel. This is less preferable as it dictates the behavior of all XML files, whereas the scope is for specific files as Excel and unspecified files to continue having default XML rendering.

SOLUTION: CUSTOM HEADERS

  • Custom Headers to specify download as an attachment can easily be achieved by creating an Http Handler, and associating the path of required files. An Http Module could be written, but that is specific to the file extension, and there is need for some XML files to be treated as text, and others as Excel. In cases like this, a Handler is much more efficient than a Module, because it’s execution is limited to the path.
  • We simply want to add a header for Content-Disposition as an attachment, and to specify the appropriate filename for download.
  • The remainder of this blog post is a demo of code for this solution. If you wish, you may download the attached ZIP file to see the completed solution.

CODE: HTTP HANDLER

 In Visual Studio 2013, create a class library project called “WebCustomHttpHandler” and add a class “DownloadFileHandler” as per code given below. Error handling / comments removed for brevity.

DownloadFileHandler.cs Copy Code

 //Add a reference to System.Web in the project references. using System.Linq; using System.Web; namespace WebCustomHttpHandler { class DownloadFileHandler : IHttpHandler { public bool IsReusable {get { return true; } } public void ProcessRequest(HttpContext context) { var filePath = context.Request.FilePath; var fileName = filePath.Split('/').Last(); context.Response.ContentType = "text/plain"; context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); context.Response.WriteFile(filePath); } } } 

CODE: WEB SITE

Add a reference within the web site to the class library project created earlier. This is not required, but will simplify deployment by ensuring the DLL for the Http Handler is copied to the bin directory of the web site.
Where applicable, simply provide a link to the XML file for download.
Edit the web.config file as per below. Adjust values for the path as required.

Web.Config Copy Code

<?xml version="1.0" encoding="UTF-8"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.5" />  </system.web>  <system.webServer>    <handlers>      <add name="ExcelHandler" verb="GET"        path="DemoExcelAsXmlSpreadsheet1.xml"        type="WebCustomHttpHandler.DownloadFileHandler, WebCustomHttpHandler"      resourceType="File" />    </handlers>  </system.webServer></configuration>

CODE: CONFIGURATION - IIS

The IIS server needs to have a Http Handler configured for the web site. This is done using the command appcmd.exe.
Reference https://technet.microsoft.com/en-us/library/jj635852.aspx
Run this in a batch file, optionally output to a log file and / or parameterize the site name, handler name, etc.

InstallHttpHandler.bat Copy Code

ECHO OFFREM Remove existing handler mapping.%windir%\system32\inetsrv\appcmd set config "DemoExcel" /section:system.webServer/handlers /-"[name='ExcelXmlFile']"REM Add new handler mapping.%windir%\system32\inetsrv\appcmd set config "DemoExcel" /section:system.webServer/handlers /+"[name='ExcelXmlFile',path='DemoExcelAsXmlSpreadsheet1.xml',verb='GET',type='WebCustomHttpHandler.DownloadFileHandler, WebCustomHttpHandler',ResourceType='File']"

REFERENCES

Appcmd.exe (IIS 8)

https://technet.microsoft.com/en-us/library/jj635852.aspx

Add a Managed Handler Mapping (IIS 7)
https://technet.microsoft.com/en-us/library/cc753249(v=WS.10).aspx

Remove a Handler Mapping (IIS 7)
https://technet.microsoft.com/en-us/library/cc754894(v=WS.10).aspx

 

CONCLUSION

It's not difficult at all to create a custom Http Handler. In design of this type of functionality, it is advisable to understand when you would want a Http Handler versus Http Module.
 

 

 

OpenExcelFileDemo.zip