如何使用mvc5在浏览器中显示docx或pdf

时间:2018-04-12 06:07:27

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

我正在研究我想在浏览器中显示docx或pdf的项目,这些文档来自数据库作为二进制数据。这是我的代码

控制器

private DiagnosticDetailModel GetFileList(int id)
        {
            var DetList = db.DiagnosticDetailModels.Where(p => p.Id == id).FirstOrDefault();
            return DetList;

        }
 [HttpGet]
        public ActionResult RetrieveFile(int id)
        {

           DiagnosticDetailModel diagnosticDetailModel = GetFileList(id);

            byte[] img = diagnosticDetailModel.FileContent.ToArray();
            string s = Encoding.UTF8.GetString(img);
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(img);

            Response.AppendHeader("Content-Disposition", "inline");
            ViewData["myInnerHtml"] = s;
               return View("test", ViewData["myInnerHtml"]);


        }

test.cshtml

<div id="doc-viewer-id" style="width:500px; height:500px;">
    @(new HtmlString(ViewData["myInnerHtml"].ToString()))


</div>

index.cshtml

<table>
    <thead style="background-color:#5bc0de; color:white;font-size:14px;">
        <tr>
            <th>#</th>
            <th>
                Diagnostic Name
            </th>
            <th>
                Registration Date
            </th>

            <th>View Report</th>
        </tr>
    </thead>
    <tbody style="font-size:12px;">
        @foreach (var item in Model)
        {
            <tr>
                <td></td>

                <td>
                    @Html.DisplayFor(modelItem => item.DiagnosticName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>

                <td>

                    @Html.ActionLink("View", "RetrieveFile", new { id = item.Id }, new { @class = "btn btn-primary btn-sm fa fa-eye", @style = "color:white;background-color:#5bc0de;",@target="_blank" })

                </td>
            </tr>
        }
    </tbody>
</table>

当我点击按钮在浏览器中显示文档时,我得到的数据如下图所示

enter image description here

有人知道我在代码中做错了吗?

0 个答案:

没有答案
相关问题