【问题标题】:ASP.NET MVC FileContentResult inline PDF filename not showing in browser titleASP.NET MVC FileContentResult 内联 PDF 文件名未显示在浏览器标题中
【发布时间】:2020-05-28 20:30:58
【问题描述】:

.NET Framework 4.7.2 MVC 项目

我有一个动态加载 PDF 以内联返回​​到浏览器的控制器操作。一切正常,除了文件名未显示在浏览器标题栏或选项卡中。相反,浏览器显示“id”参数的值,大概是因为它跟在最后一个斜杠后面。

我需要浏览器显示 PDF 文件名,以便用户知道他们正在查看的 PDF。我们的用户倾向于在选项卡中打开多个 PDF 并经常在它们之间切换。这是代码的简化版本:

public FileContentResult ViewPDF(int id)
{
    byte[] pdfContents = loadPDF(id);
    string filename = getPDFFilename(id);

    var cd = new ContentDisposition
    {
        FileName = filename,
        Inline = true
    };

    Response.AddHeader("Content-Disposition", cd.ToString());

    string returnContentType = MimeMapping.GetMimeMapping(filename);            

    return File(pdfContents, returnContentType);
}

我也尝试过 FileStream,但没有成功,还尝试过 Edge、IE 和 Chrome,但它们都做同样的事情。在 Chrome 中,当我保存 PDF 时,它会以正确的文件名保存,但不会在浏览器中显示。

【问题讨论】:

    标签: c# asp.net asp.net-mvc pdf filecontentresult


    【解决方案1】:

    这不是理想的解决方案,但我最终创建了一个包含全屏<iframe> 的新视图页面,该页面可以内联加载 PDF。这样我至少可以在页面标题中显示 PDF 名称,它出现在选项卡中。

    与此类似:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>@ViewData["Title"]</title>
        <style type="text/css">
            body, html {
                width: 100%;
                height: 100%;
                overflow: hidden;
                margin: 0;
            }
    
            .pdfFrame {
                width: 100%;
                height: 100%;
                border: none;
            }
        </style>
    </head>
    
    <body class="nomargins">
        <iframe class="pdfFrame" src="@Url.Action("GetPDF", new { id = ViewBag.pdfID })" frameBorder="0"></iframe>
    </body>
    
    </html>
    

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多