【问题标题】:Rendering PDF in browser - the PDF is showing up when running under "localhost", but NOT showing up when replacing "localhost" with my machine name在浏览器中渲染 PDF - 在“localhost”下运行时显示 PDF,但在用我的机器名称替换“localhost”时不显示
【发布时间】:2013-06-14 22:03:01
【问题描述】:

我有一个 MVC 应用程序,它显示从数据库中检索的字节数据的 PDF,如果我的浏览器指向“localhost”,PDF 在我的互联网浏览器中呈现得很好。但是,如果我用我的实际机器名称更改“localhost”,则 PDF 不会呈现。该应用程序完全按照我的预期工作,除了这一点。顺便说一下(显然),我的网站托管在本地 IIS 中。

有趣的是,当 PDF 不呈现时,我可以右键单击 PDF 应该呈现的区域并选择“另存为”并将 PDF 保存到我的计算机,打开它,它只是打开美好的。

问题:任何人都可以就可能导致问题的原因提供任何建议吗?在 IIS 托管方面,“localhost”是否完全等同于我的机器名称?

我不确定这是否重要,但这是我的代码:

控制器代码

public FileStreamResult PdfGenerator(string id)
{
    Stream fileStream = GeneratePdf(id);
    HttpContext.Response.AddHeader("content-disposition", "attachment; filename=form.pdf");
    return new FileStreamResult(fileStream, "application/pdf");
}

public Stream GeneratePdf(string id)
{
    // get and return the PDF byte data
    byte[] pdf = (from i in db.Pictures where i.GroupId == id && i.blah== "YES" select i.pdf).FirstOrDefault();
    Stream pdfStream = new MemoryStream(pdf);
    return pdfStream;
}

public ActionResult DisplayPdf(string id)
{
    Picture picture = (from i in db.Pictures where i.GroupId == id && i.blah== "YES" select i).FirstOrDefault();
    return View(picture);
}

DisplayPdf 视图

@model blah.Models.Picture

    <object 
        data="@Url.Action("PdfGenerator", "ImageViewer", new { id = Model.GroupId })"
        type="application/pdf" 
        width="640" 
        height="480">
        <param value="transparent" name="wmode"/>
    </object>

【问题讨论】:

  • +1 只是为了我见过的最长的标题! :)
  • @Liam 我还有很多其他标题很长的问题,请随意投票,:) 哈哈
  • 我不确定,我会假设在涉及 IIS 时,“localhost”在所有方面都等同于我的机器名称......不过我可能是错的。
  • 不,忘记了,说它不适用于object
  • 当您浏览到 url 时会发生什么?有用吗?

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


【解决方案1】:

奇怪的是,这是一个浏览器兼容性问题。这当然不能解释为什么它在 localhost 和我的机器名称下工作,但我所做的是在我的 object 标签中添加以下内容:

<embed src="@Url.Action("PdfGenerator", "ImageViewer", new { id = Model.GroupId })" type="application/pdf" height="1024" width="768" />  

所以,如果浏览器不支持&lt;object&gt;,那么它会渲染&lt;embed&gt;标签。

【讨论】:

    【解决方案2】:

    下午好,尝试将以下内容添加到您的 PdfGenerator 函数中:

    HttpContext.Response.AddHeader("X-Frame-Options", "SAMEORIGIN");
    

    您是否在 IFRAME 中呈现 PDF?

    【讨论】:

    • 不,我只是在对象标签中呈现 PDF。我真的按照你所说的那样输入“SAMEORIGIN”吗?
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2012-04-11
    • 2014-06-25
    • 2019-09-08
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多