Using FileStreamResult to display PDF in browser

1.7k Views Asked by At

I am trying to display a PDF file in a browser using the action below. When this is executed the result is an entire screen that looks like the image attached. Rendered result It looks like MediaTypeNames.Application.Pdf is being ignored. The same result with Chrome or Firfox. What I am missing?

[HttpGet]
    public FileResult ViewFile()
    {
        Response.AppendHeader("Content-Disposition", "inline; filename=" + Server.UrlEncode("file.pdf") + ";");
        var path = @"C:\temp\file.pdf";
        FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        return new FileStreamResult(stream, MediaTypeNames.Application.Pdf);
    }
1

There are 1 best solutions below

5
AudioBubble On

You can use below below code snippet to display a PDF document in browser.

        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        fileStream.Position = 0;
        return new FileStreamResult(fileStream, System.Net.Mime.MediaTypeNames.Application.Pdf);