【问题标题】:ASP.NET Core How to open PDF in a new tab?ASP.NET Core 如何在新标签页中打开 PDF?
【发布时间】:2021-08-08 21:55:22
【问题描述】:

在我的控制器中,我有以下操作来创建 PDF

public async Task<IActionResult> ExportMailingLabel(int CustomerID, int ProductID)
        {
            var mailingLabel = await NoticeService.CreateMailingLabel(CustomerID, ProductID);
            return File(mailingLabel.NoticeContents, "application/pdf", "MailingLabel.pdf");
        }

在我看来,我有以下链接,

<a asp-action="ExportMailingLabel" asp-controller="Product" asp-area="Product" asp-route-CustomerID="@Model.CustomerID" asp-route-ProductID="@Model.ProductID" class="btn btn-primary"><i class="fa fa-receipt"></i> View Mailing Label</a>

单击以在新选项卡中打开 PDF 而不是显示打开对话框时,我需要帮助。

我试过target="_blank",但我似乎打开了一个新标签,但仍然显示打开的对话框

【问题讨论】:

  • 嗨@Rob,这取决于浏览器设置如何处理下载。我建议您可以使用Chrome浏览器。它可以打开一个新标签而不显示打开的对话框。
  • 嗨@Rena 我刚刚试过了,chrome 会自动下载它,Firefox 会为您提供保存或打开的选项。谢谢!

标签: asp.net-core


【解决方案1】:

_target="blank" 是一个简单的 HTML 标签,我认为它可以在所有浏览器中按预期工作。您可以将其与静态或动态文件名一起使用,如下所示。

静态文件名用法

Controller.cs

public async Task<IActionResult> ExportMailingLabel(int CustomerID, int ProductID) {
    var mailingLabel = await NoticeService.CreateMailingLabel(CustomerID, ProductID);
    return File(mailingLabel.NoticeContents, "application/pdf");//we don't send 3.parameter yet
}

View.cshtml

<a asp-action="ExportMailingLabel"
   asp-controller="Product"
   asp-route-CustomerID="@Model.CustomerID"
   asp-route-ProductID="@Model.ProductID"
   asp-route-FileName="MailingLabel.pdf" class="btn btn-primary" id="btnOpenDocument">
    <i class="fa fa-receipt"></i> View Mailing Label
</a>

@section Scripts
{
    <script>
        //We are opening the file with js instead of action when click to the button
        $('#btnOpenDocument').click(function (e) {
            e.preventDefault();
            window.open('@Url.Action("ExportMailingLabel"
                             ,"Product"
                             ,new {customerId=selectedCustomerId
                                  ,productId=selectedProductId
                                  ,fileName="MailingLabel.pdf" })'
                        ,"_blank");
        });
    </script>
}

动态文件名用法

Controller.cs

//We are adding a new route to action for file name
[HttpGet("[controller]/[action]/{customerId}/{productId}/{fileName}")]
public async Task<IActionResult> ExportMailingLabel(int CustomerID, int ProductID) {
    var mailingLabel = await NoticeService.CreateMailingLabel(CustomerID, ProductID);
    return File(mailingLabel.NoticeContents, "application/pdf", $"{CustomerID}_{ProductID}.pdf");        
}

View.cshtml

<a asp-action="ExportMailingLabel"
   asp-controller="Product"
   asp-route-CustomerID="@Model.CustomerID"
   asp-route-ProductID="@Model.ProductID"
   asp-route-FileName="@(Model.CustomerID)_@(Model.ProductID).pdf" class="btn btn-primary" id="btnOpenDocument">
    <i class="fa fa-receipt"></i> View Mailing Label
</a>

@section Scripts
{
    <script>
        //We are opening the file with js instead of action when click to the button
        $('#btnOpenDocument').click(function (e) {
            e.preventDefault();
            window.open('@Url.Action("ExportMailingLabel"
                             ,"Product"
                             ,new {customerId=selectedCustomerId
                                  ,productId=selectedProductId
                                  ,fileName=selectedCustomerId+"_"+selectedProductId+".pdf" })'
                        ,"_blank");
        });
    </script>
}

FileContentResult Class

【讨论】:

  • 这种情况下有没有办法设置文件名?我试过了,它工作了,但文件名只是控制器方法名,这不是我想要调用的文件。
  • @trademark 是的,你是对的。在您提出问题后,我在帖子内容中添加了另一行。您可以从 DYNAMIC USAGE 标题 中使用它
  • 也许我遗漏了一些东西,但添加第三个参数不会导致文件只是下载而不是在浏览器中打开?我读过使用该参数将 Content-Disposition 设置为“附件”,这会使浏览器下载它。 stackoverflow.com/a/54996397/5354201
  • @trademark 我之前没有检查参数,但我刚才看了一下。您可以从here 和示例here 看到它。您可以为 dispositon 添加一个标头,但它已经是 inline。我不需要提及,因为 SO 与它无关。
猜你喜欢
  • 1970-01-01
  • 2018-11-05
  • 2016-10-21
  • 2022-01-16
  • 2020-05-31
  • 2021-06-02
  • 2018-11-04
  • 2017-05-24
  • 1970-01-01
相关资源
最近更新 更多