Created
May 20, 2015 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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