【问题标题】:Dynamically Set title of page动态设置页面标题
【发布时间】:2014-02-19 13:11:15
【问题描述】:

我想将返回 PDF 文件流的网页标题设置为:

public ActionResult PrintInvoice(long ID)
{
    var data = db.Documents.Where(x => x.InvoiceNumber == ID);
      ReportDocument rd = new ReportDocument();
      rd.Load(Server.MapPath("~/Reports/InvoiceDocument.rpt"));

    Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    stream.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(stream, "application/pdf");               //For Showing PDF in Browser itself
}

我想在这个页面上设置标题。

如何设置此页面的标题。

目前页面上的标题如下图所示::

【问题讨论】:

  • 使用@cubitouch 答案(这是正确的),您将无法更改标题,我认为没有办法做到这一点。您将得到的是该文件将在浏览器中打开,当您保存它时,它将获得您在 File() 调用的第三个参数中指定的文件名。
  • 这里有一篇文章再次说明你想要达到的目标是做不到的:forums.asp.net/t/1707949.aspx。解决方法在底部进行了说明:使用IFRAME 将 pdf 渲染到其中。
  • 浏览器(至少 Chrome 和 Firefox)从 pdf 文档的标题元数据中设置页面标题。 w3.org/TR/WCAG20-TECHS/PDF18.html

标签: c# asp.net-mvc asp.net-mvc-3 web.config-transform


【解决方案1】:

查看 HTTP 标头,例如 thread

试试类似的东西:

Response.AppendHeader("Content-Disposition", "inline; filename=your page title");

也看看这个推荐的thread

return File(stream, "application/pdf", "your page title");

请记住,这种数据可以在不同的浏览器中以不同的方式执行。

【讨论】:

  • 但它响应文件下载。我只想在网页上显示标题。
  • Content-Disposition 也应该指定inline;stackoverflow.com/questions/3724278/…
  • @cubitouch 对标题没有任何影响。
  • @RahulRJ 页面的当前标题到底是什么?
  • @cubitouch 我已经编辑了我的帖子,其中我附上了我在页面上获得的标题图片。
【解决方案2】:

这就是我最终在我的情况下所做的。

下面的控制器代码包含两个动作。第一个操作返回一个模型,我可以使用它来设置页面标题(这可能只是一个字符串,具体取决于您的用例)。第二个动作是获取文件内容。就我而言,我将文件内容存储在数据库中,因此我使用 id 来获取文档。

第二个操作还设置响应标头,以便在他们尝试下载文件时正确显示文件名。

    public IActionResult PreviewDocument(int id)
    {
        Document document = _legislationFolderService.GetDocument(id);

        if (document == null)
            return NotFound($"Could not find document with id of {id}");

        return View(document);
    }

    public IActionResult PreviewDocumentContents(int id)
    {
        DocumentContents documentContents = _legislationFolderService.GetDocumentContents(id);

        if (documentContents == null)
            return NotFound($"Could not find contents for document with id of {id}");

        Response.Headers.Add("Content-Disposition", $"inline; filename={documentContents.Document.Name}.pdf");
        return new FileStreamResult(new MemoryStream(documentContents.Contents), "application/pdf");
    }

在下面的视图 (PreviewDocument.cshtml) 中,我使用 iframe 填充页面并链接到 PreviewDocumentContents 操作。我不希望我的主模板中包含布局,所以我将其设置为 null 并为页面建立了一个基本的 html 结构,我在 html 中设置了标题。

@model EFloorFiles.Service.Models.Document

@{
    Layout = null;
    ViewBag.Title = Model.Name;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewData["Title"] - E-Floor Files</title>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
        }

        iframe {
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>
<body>
    <iframe src="@Url.Action("PreviewDocumentContents", new { id = Model.Id })"></iframe>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2022-10-06
    • 2014-08-16
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2015-08-17
    • 1970-01-01
    • 2010-09-10
    相关资源
    最近更新 更多