繁体   English   中英

在 .NET MVC 中的新选项卡中打开一个 PDF 文件

[英]Open a PDF file in a new tab in .NET MVC

我有一个生成 PDF 文件的按钮。 我试图在新的 window 中打开 PDF 文件,但到目前为止我一直没有成功。

按钮代码:

<button type="button" class="btn btn-warning" onclick="location.href = '@Url.Action("/DetailToPdf/"+ comb.Id, "Combined")'"
                        data-placement="top" data-container="body" data-toggle="popover" data-content="PDF" data-trigger="hover">
                    <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>
                </button>

我的controller的相关代码:

public ActionResult DetailToPdf(long id = 0)
    {
        ...

        //Convert HTML string to PDF
        PdfDocument document = htmlConverter.Convert(htmlText, baseUrl);
        
        MemoryStream stream = new MemoryStream();

        //Save and close the PDF document 
        document.Save(stream);

        document.Close(true);

        return File(stream.ToArray(), "application/pdf", model.Id + "_approval.pdf");
    }

到目前为止我尝试过的事情:

formtarget="_blank"添加到按钮标签

添加target="_blank"到按钮标签

添加, new { target = "_blank" }到 url.action

这些似乎都不起作用。 我在这里错过了什么/做错了什么?

我设法通过将按钮标签更改为标签来修复它:

<a href="@Url.Action("/DetailToPdf/"+ comb.Id, "Combined")"  class="btn btn-warning" target="_blank"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span></a>

我会让这个问题再开放一段时间,看看人们是否知道关于这个问题的更好方法 go。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM