I have an MVC3 app that allows the user to dowload a file to their hard drive or view it in a browser. The file is stored on a filesher thats not accessible to the internet. So the file’s byte array is stored in memory and then sent to the browser like this:
/// <summary>
/// overrides actionresult method so document can be viewed in browser window
/// </summary>
/// <param name="context"></param>
public override void ExecuteResult(ControllerContext context)
{
// get current context and set values
var response = context.HttpContext.Response;
response.Clear();
response.BufferOutput = false; // to prevent buffering
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = ContentType;
response.AddHeader("Content-Length", Length.ToString());
var ext = Path.GetExtension(FileName);
if (!Show)
response.AddHeader("content-disposition", "attachment; filename=\"" + FileName+ "\"");
response.ContentEncoding = System.Text.Encoding.UTF8;
var stream = new MemoryStream(ContentBytes);
stream.WriteTo(response.OutputStream);
stream.Dispose();
}
I cant view pdfs for some reason. I can download them but not view them. Im attaching a sample project here showinbrowser.zip. Its VS2010 sp1. Can someone show me how to display a PDF file in a new browser window/tab thats stored in memory? Thanks
Why not use the
Controller.Filemethod which handles this for you ?