用html5将byte []显示为pdf

时间:2013-06-24 13:13:17

标签: c# html5 pdf asp.net-mvc-4 razor

嗨,我有一个大问题。 我有一个来自Wcf服务的byte [] receveid。字节数组表示pdf文件。 在Controller中,我想这样做:

PDFDto pdfDTO = new PDFDTO();
pdfDTO.pdfInBytes = pdfInBytes; //the byte array representing pdf
return PartialView("PopupPDF,pdfDTO);

在视图中我想这样做:

      <object data="@Model.pdfInBytes" width="900" height="600" type="application/pdf"> </object>

但似乎不正确。 我在互联网上搜索过,有很多关于这个问题的帖子,但没有人能完全匹配我的情况。 你有什么想法吗?非常感谢!

2 个答案:

答案 0 :(得分:9)

我建议在控制器designed specifically for rendering the PDF data上创建另一个操作。然后,只需在data属性中引用它:

data="@Url.Action("GetPdf", "PDF")"

我不确定您是否可以在DOM中转储原始PDF数据而不会遇到某种编码问题。尽管我从未尝试过,但您可能会像图像一样。但是,为了演示,图像看起来像:

src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"

所以,假设可以做同样的事情,它可能如下所示:

@{
  String base64EncodedPdf = System.Convert.ToBase64String(Model.pdfInBytes);
}

@* ... *@
<object data="data:application/pdf;base64,@base64EncodedPdf"
        width="900" height="600" type="application/pdf"></object>

我仍然坚持原来的回应来创造新的行动。

答案 1 :(得分:0)

protected ActionResult InlineFile(byte[] content, string contentType, string fileDownloadName)
    {
        Response.AppendHeader("Content-Disposition", string.Concat("inline; filename=\"", fileDownloadName, "\""));
        return File(content, contentType);
    }

将此代码添加到基本控制器或控制器本身,并调用此函数在浏览器中显示文件。

相关问题