How to return a file but not attachment in .net web api?

mc 5,511 Reputation points
2022-01-26T05:34:19.773+00:00

I am using .net 6 web api and I want to response a pdf file.

I want to display a pdf file in ie11 but it will download directly.

so I google and get a answer that I should set "Content-Disposition" to "inline" not "attchment" but still failed.

how to return a file that ie11 will not open but display it?

[HttpGet]
        public IActionResult Get()
        {
            var file = System.IO.File.ReadAllBytes("1.pdf");

            HttpContext.Response.Headers.ContentDisposition = "inline;filename=1.pdf";


            return File(file, "application/pdf", "1.pdf");
        }
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Poblacion 1,571 Reputation points
    2022-01-26T07:38:15.537+00:00

    Most likely it is not a problem in your code but in IE. Contrary to modern browsers, IE does not have a built-in PDF viewer, you need to install it as a plug-in from third parties. For instance, if I recall correctly when you install Acrobat Reader it adds a plugin for IE.
    If the plugin is not installed, then IE treats it as an "unknown file type" and just downloads it without displaying.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.