0

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);
    }
haler
  • 3
  • 4

1 Answers1

0

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);
  • I added fileStream.Position = 0; but it still produces the same result as shown in the attached image. It looks like the browser thinks the file is text not a PDF. – haler Mar 24 '18 at 20:16
  • try by opening the file directly in browser, check it is displayed properly or not. –  Mar 25 '18 at 13:26
  • The file is a valid pdf the browser is not rendering as PDF. I get �/��$Z����U�m@�� – haler Mar 26 '18 at 13:47
  • If use a partial view that contains a embed the pdf is correctly displayed. I also tried returning File(pdfByte, MediaTypeNames.Application.Pdf); and the garbage chars are displayed. What am I missing? – haler Mar 26 '18 at 15:19
  • I can't use embed as the pdf is when a stream. – haler Mar 26 '18 at 15:21