Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

c# - Open PDF file in new browser window in ASP.NET Core 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...