Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Created May 20, 2015 14:47
Show Gist options
  • Save AshleyGrant/351072556fa6c631fa8e to your computer and use it in GitHub Desktop.
Save AshleyGrant/351072556fa6c631fa8e to your computer and use it in GitHub Desktop.
ASP.Net MVC 5 FileContentResult that handles IE8. Can't claim credit for this. Putting here for anyone to use
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AshleyGrant.Web
{
public class FileResultThatHandlesIE8 : FileContentResult
{
private readonly string _fileName;
public FileResultThatHandlesIE8(byte[] fileContents, string contentType, string fileName = null)
: base(fileContents, contentType)
{
_fileName = fileName;
}
public override void ExecuteResult(ControllerContext context)
{
if (_fileName != null && String.IsNullOrWhiteSpace(base.FileDownloadName))
base.FileDownloadName = _fileName;
var b = context.HttpContext.Request.Browser;
if (b != null && b.Browser.Equals("ie", StringComparison.OrdinalIgnoreCase) && b.MajorVersion <= 8)
{
context.HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(base.FileDownloadName) + "\"");
WriteFile(context.HttpContext.Response);
}
else
{
base.ExecuteResult(context);
}
}
}
}
// in any action simply use this like a normal FileResult
return new FileResultThatHandlesIE8(bytes, mimeType, fileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment