I had this code in an old asp.net webforms app to take a MemoryStream
and pass it as the Response showing a PDF as the response. I am now working with an asp.net MVC application and looking to do this this same thing, but how should I go about showing the MemoryStream
as PDF using MVC?
Here's my asp.net webforms code:
private void ShowPDF(MemoryStream ms)
{
try
{
//get byte array of pdf in memory
byte[] fileArray = ms.ToArray();
//send file to the user
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Page.Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = string.Empty;
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", fileArray.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment;filename=TID.pdf;");
Response.BinaryWrite(fileArray);
Response.Flush();
Response.Close();
}
catch
{
// and boom goes the dynamite...
}
}