Convert asp.net web forms logic to asp.net MVC

I had this code in an old asp.net webforms app to take MemoryStream

and feed it as a response showing the PDF as the response. I am now working with ASP.NET asp.net application and want to do it the same way, but how should I display MemoryStream

as PDF using MVC?

Here's my asp.net webform 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...
        }
    }

      

+2


a source to share


1 answer


Here is a blog post: http://biasecurities.com/blog/2008/binaryresult-for-asp-net-mvc/



UPDATE: The last comment on this post mentions Response.TransmitFile, you may need to adapt your code to use if your PDFs are large and you will have many concurrent uploads.

+2


a source







All Articles