【问题标题】:Open PDF file in new browser window in ASP.NET Core 3在 ASP.NET Core 3 的新浏览器窗口中打开 PDF 文件
【发布时间】:2021-01-07 03:52:28
【问题描述】:

我在 ASP.NET Core 3 的 MVC 控制器中创建了 pdf 文件。我正在从该控制器返回 FileStreamResult。 我想在新的浏览器窗口中打开 PDF。

以下是 HTML 代码 -

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

下面是控制器

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

我正在获取 pdf 文件的下载选项。相反,我想在新的浏览器窗口中打开 pdf。

【问题讨论】:

  • 请注意,[asp.net] 和 [asp.net-core] 是互斥的(ASP.NET 用于 .NET Framework 项目,而 ASP.NET-CORE 用于 .NET Core 项目)。另请注意,如果您使用的是 ASP.NET Core,则有一个 .NET Core MVC 标记 ([asp.net-core-mvc])。
  • 您可能需要找到一种设置“内联”的 ASP.NET/ASP.NET Core 方式(请参阅here)。
  • 您无法决定如何在客户端 PC 上打开 PDF。下载后,您将无法控制它。

标签: c# asp.net-core pdf asp.net-core-mvc


【解决方案1】:

如果您想在新标签页中打开 pdf,请不要指定 fileDownloadName。

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 2019-01-07
相关资源
最近更新 更多