3

I have created the pdf file in the MVC Controller in ASP.NET Core 3. I am returning the FileStreamResult from this controller. I want to open the PDF in a new browser window.

Below is the HTML Code -

<a asp-controller="Score" asp-action="ViewScore" target="_blank" >View PDF</a>

Below is the controller

public async Task<IActionResult> ViewScore()
{
      /*Created the pdf doc*/
      MemoryStream stream = new MemoryStream();
      stream = doc1.Stream;

      string contentType = "application/pdf";
      string fileName = "sample.pdf";

      var file = File(stream, contentType, fileName);
            
      return file;
}

I am getting the downloading option for the pdf file. Instead of that, I want to open the pdf in the new browser window.

Adi
  • 405
  • 1
  • 7
  • 13
  • Note that `[asp.net]` and `[asp.net-core]` are mutually exclusive (ASP.NET is for .NET Framework projects, whereas ASP.NET-CORE is for .NET Core projects). Note also that, if you're using ASP.NET Core, there is a .NET Core MVC tag for that (`[asp.net-core-mvc]`). – ProgrammingLlama Jan 06 '21 at 07:20
  • It seems like you might need to find an ASP.NET/ASP.NET Core way of setting "inline" (see [here](https://stackoverflow.com/questions/6293893/how-do-i-force-files-to-open-in-the-browser-instead-of-downloading-pdf)). – ProgrammingLlama Jan 06 '21 at 07:21
  • You cannot decide how a PDF will be opened on a client PC. Once downloaded you have no control over it. – VDWWD Jan 06 '21 at 07:28

1 Answers1

3

Don't specify fileDownloadName if you want to open the pdf in a new tab.

public async Task<IActionResult> ViewScore()
{
     /*Created the pdf doc*/
     MemoryStream stream = new MemoryStream();
     stream = doc1.Stream;

     string contentType = "application/pdf";
     string fileName = "sample.pdf";

     var file = File(stream, contentType);
        
     return file;
}
mj1313
  • 7,930
  • 2
  • 12
  • 32
  • Hi @Adi, if my solution can work, could you please [accept it as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), thanks! – mj1313 Jan 20 '21 at 01:51