【问题标题】:C# MVC: Chrome using the action name to set inline PDF titleC# MVC:Chrome 使用动作名称设置内联 PDF 标题
【发布时间】:2016-04-25 17:51:17
【问题描述】:

我有一个在新浏览器选项卡中显示 PDF 的操作。

    public ActionResult Print()
    {
        var cd = new ContentDisposition
        {
            FileName ="something.pdf",
            Inline = true 
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(reportResponse.Data.Document, MediaTypeNames.Application.Pdf);

    }

文件名工作正常。当我下载文件时,它的名称是我想要的“something.pdf”。

问题是当 google chrome 在新的浏览器选项卡中打开 PDF 时,它会将控制器操作名称(打印)显示为 PDF 的标题。这就是我想要改变的。我附上一张图片以供澄清。

查看代码: Url.Action("Print", "Controller", new { area = "Area" }), new { @target = "_blank" }

【问题讨论】:

  • 浏览器(至少 Chrome 和 Firefox)从 pdf 文档的标题元数据中设置页面标题。 w3.org/TR/WCAG20-TECHS/PDF18.html
  • 就我而言,PDF 是与其他内容内联显示的部分页面。在 MVC 中,标题设置为动作的名称。就我而言,我只是将动作名称更改为通用名称。仍然希望找到更好的解决方案。

标签: c# asp.net-mvc google-chrome pdf


【解决方案1】:

添加到 kodandarami 的答案,

当您从 ASP.NET MVC 传回 PDF 文件时,页面标题设置为用于到达控制器的路由配置的最后一个 url 部分

知道了这一点,您可以通过将参数设置为控制器的路由配置的一部分来动态命名标题,例如:

        routes.MapRoute(
            "Print",
            "print/{pdfName}",
            new { controller = "Print", action = "Index", pdfName = UrlParameter.Optional }
        );

然后 MVC 将使用这个值,因为它认为它是一个单独的页面。

在网址中使用它作为 "/print/this-is-my-pdf-title" 不是 '/print?pdfName=this-is-my-pdf-title"

作为查询字符串参数,MVC 将回退到调用 PDF 'print'。

注意: 正如this related question中提到的,

当文本在查询字符串'?'之前时,IIS 对禁止字符有更严格的规则。特点。你不允许<,>,*,%,&,:,\ (See MS Reference)

这些字符中的任何一个,即使在编码后都会给您一个“路径有潜在危险”400错误。

确保删除/替换这些禁用字符。

.

【讨论】:

    【解决方案2】:

    将文件名的参数传递给action方法,并确保参数名称为'id'

    查看代码:

    Url.Action("Print", "Controller", new { id = "filename", area = "Area" }), new { @target = "_blank" }

    【讨论】:

    • 在没有文件名作为id 属性的情况下是否有机会完成这项工作?我有一个数字 id(例如 1234),并且 firefox/chrome 都显示带有这些数字 id 而不是文件名的 pdf。尽管设置了Inline = true,但您的 sn-p 下载 pdf 文件而不是内联显示它们。
    【解决方案3】:

    我通过使用 iframe 解决了这个问题。

    创建一个填充标题和 pdf url 的操作。

            public ActionResult ArticleSpecification(int ArticleID)
            {
                using (var context = new myEntities())
                {
                    Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                    ViewData["Title"] = article.Code + " - " + article.Description;
                    ViewData["PdfSource"] = "ArticleSpecificationPdf?ArticleID=" + article.ArticleID;
                    return View("~/Views/Specification/pdfview.cshtml");
                }
            }
    

    pdfview.cshtml:带有 iframe 来查看 pdf 和标题。

    @{
        Layout = "";
    }
    <!DOCTYPE html>
    <html>
        <head>
            <title>@ViewData["Title"]</title>
            <style>
                html, body, iframe {
                    height: 100%;
                    width: 100%;
                    margin: 0;
                    border: 0;
                }
                body {
                    overflow-y: hidden;
                }
            </style>
        </head>
        <body>
            <iframe src="@ViewData["PdfSource"]"></iframe>
        </body>
    </html>
    

    返回 pdf 内容的操作。

            public FileResult ArticleSpecificationPdf(int ArticleID)
            {
                using (var context = new myEntities())
                {
                    PdfFile file = null;
                    Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                    if (article != null && article.SpecificationPdfID != null)
                        file = context.PdfFile.FirstOrDefault(a => a.PdfFileID == article.SpecificationPdfID);
                    return new FilePathResult(file.path, "application/pdf");
                }
            }
    

    【讨论】:

      【解决方案4】:

      试试下面的返回语句:

      return File(reportResponse.Data.Document, MediaTypeNames.Application.Pdf, "something.pdf");
      

      【讨论】:

      • 这会下载具有该名称的文件,而不是在浏览器中显示它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2016-12-14
      相关资源
      最近更新 更多