预览PDF文件(读取文件流方式)

 @if (list != null && list.Count>0)
            {
                <tr>
                    <td>申请表</td>
                    <td><a href="/policy/declare/readPDF/?coinfigid=@ViewBag.coinfigid" target="_blank" class="layui-btn layui-btn-normal layui-btn-xs">预览</a></td>
                </tr>
                if (ViewBag.hascheck == 1)
                {
                    <tr>
                        <td>确认单</td>
                        <td><a href="/policy/declare/readPDF/?coinfigid=@ViewBag.coinfigid&type=1" target="_blank" class="layui-btn layui-btn-normal layui-btn-xs">预览</a></td>
                    </tr>
                }
                foreach (var item in list)
                {
                    <tr>
                        <td>@item.contractname</td>
                        <td><a href="/policy/declare/readPDF/?id=@item.ID&coinfigid=@ViewBag.coinfigid" target="_blank" class="layui-btn layui-btn-normal layui-btn-xs">预览</a></td>
                    </tr>
                }
            }
服务端:
/// <summary>
        /// 读取PDF文件
        /// </summary>
        /// <param name="fName">文件名称(可以从其他地方传进来)</param>
        /// <returns></returns>
        public FileStreamResult readPDF(string coinfigid, int id = 0, int ischeck = 0)
        {
            var model = dbContext.policy_declaredata.FirstOrDefault(m => m.username == LoginUser);
            var hetong = dbContext.policy_tradeproject.FirstOrDefault(m => m.ID == id && m.username == LoginUser);
            if (model == null)
            {
                GoTo404();
            }
            if (id != 0 && hetong == null)
            {
                GoTo404();
            }
            string pdfurl = string.Empty;
            string filename = string.Empty;
            if (id == 0)
            {
                if (ischeck == 1)
                {
                    //确认单
                    pdfurl = System.Web.HttpContext.Current.Server.MapPath("/files/policy/" + LoginUser +
                         "/" + coinfigid );
                    filename = "check.pdf";
                }
                else
                {
                    //申请表
                    pdfurl = System.Web.HttpContext.Current.Server.MapPath("/files/policy/" + LoginUser +
                         "/" + coinfigid );
                    filename = "info.pdf";
                }
            }
            else
            {
                pdfurl = System.Web.HttpContext.Current.Server.MapPath("/files/policy/" + LoginUser +
                         "/" + coinfigid );
                filename= hetong._id.ToString() + ".pdf";
            }
            DirectoryInfo mydir = new DirectoryInfo(pdfurl);
            string pdfSrc = string.Empty;
            foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                {
                    FileInfo fi = (FileInfo)fsi;
                    string x = System.IO.Path.GetDirectoryName(fi.FullName);
                    string s = System.IO.Path.GetExtension(fi.FullName);
                    if (fi.Name == filename)
                    {
                        pdfSrc = x + "\\" + fi.Name;//pdf路径
                        ViewBag.title = fi.Name;//网页标题
                    }
                }
            }
            FileStream fs = new FileStream(pdfSrc, FileMode.Open, FileAccess.Read);
            return File(fs, "application/pdf");
        }

 

posted on 2018-10-19 14:04  阳光秋天  阅读(9211)  评论(0编辑  收藏  举报