zoukankan      html  css  js  c++  java
  • 预览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");
            }
  • 相关阅读:
    一些大牛的博客推荐,排名不分先后
    HTTP响应状态码
    myeclipse连接mysql数据库详细步骤
    widget使用与生命周期
    ExcelUtil
    POI 数据toEXCEL
    Android异步下载图片并且缓存图片到本地
    判断网络状态
    调节屏幕亮度
    spring的基本知识
  • 原文地址:https://www.cnblogs.com/sophiel/p/9816087.html
Copyright © 2011-2022 走看看