I’m using ASP.Net MVC3, and serving up files using this:
public class ByteResult : ActionResult
{
public String ContentType { get; set; }
public byte[] Bytes { get; set; }
public ByteResult(byte[] sourceStream, String contentType)
{
Bytes = sourceStream;
ContentType = contentType;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.Clear();
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = ContentType;
var stream = new MemoryStream(Bytes);
stream.WriteTo(response.OutputStream);
stream.Dispose();
}
}
With photos embedded in a page using this method, when a user selects “save picture/image as …” from a context menu, the save as dialog presents the file-name from the img src attribute for all browsers except IE (of course), which presents “untitled.bmp” . yuck.
How do I fix for IE??
There is no need for you to be creating your own ActionResult to return byte content. It already exists, it’s called
FileResult.In your controller you would use the
Filemethod like so:The 3rd parameter sets the file name in the content disposition.
Setting the content disposition yourself would work; but this is built into ASP.NET, and handles things according to the RFC 2183. For example, it handles proper escaping if the file name contains a quote.