我正在尝试使用 Asp.net mvc Controller 在新选项卡中打开 pdf。 它在新选项卡中正确打开,但未显示文档名称 这是我的代码
public ActionResult ViewDocument()
{
byte[] docFileArr=......
var cntdn = new System.Net.Mime.ContentDisposition
{
FileName = DocumentName,
Inline = true,
};
Response.AppendHeader("Content-Disposition", cntdn .ToString());
return File(docFileArr, "application/pdf");
}
文档在新窗口中正确打开,但它仅显示 Controller 名称而不显示文档名称。请参阅附件。
最佳答案
实现此目的的一种方法是在 PDF 的元数据中设置标题(在创建 PDF 期间或在下载开始之前操作 PDF 期间)。已经给出的 SO 问题已经涵盖了这一点。
另一种方法是打开一个新选项卡,加载由 ASP.NET MVC View 生成的自己的 HTML 页面,该页面本身嵌入了由 ViewDocument 操作提供的 PDF 文件(更合适的名称是 DownloadDocument)。
HTML 页面的 HTML header 中将有一个标题,该标题将由浏览器显示。例如。 View 可能看起来像(只是为了给出想法):
@{
Layout = null;
}
<!DOCTYPE html>
<html height="100%">
<head>
<meta charset="utf-8" />
<title>@Model.Caption</title>
<script src="/js/pdfobject.js"></script>
<style>
.pdfobject-container {
height: 96vh;
width: 99vw;
}
</style>
</head>
<body height="100%">
<div id="pdf" height="100%"></div>
<script>PDFObject.embed("@Model.DownloadUrl", "#pdf");</script>
</body>
</html>
它使用PDObject.js库,使 PDF 嵌入更容易且不易出错。
关于c# - 在新浏览器选项卡中查看 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628065/