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");
            }
  • 相关阅读:
    Java线程优先级(Priority)
    Java同步锁(synchronized)、锁(lock)以及死锁
    Java实现多线程的三种方式(3) ------实现Callable<V>接口
    Java实现多线程的三种方式(2) ------实现Runnable接口
    Java实现多线程的三种方式(1) ------继承Thread类
    Spring AOP(2) --基于配置文件方式配置
    Spring AOP(1) --基于注解方式配置
    Spring IOC容器基于注解方式装配Bean
    Spring IOC容器基于配置文件装配Bean(9) ------bean的SpEL用法
    Python基础教程学习目录
  • 原文地址:https://www.cnblogs.com/sophiel/p/9816087.html
Copyright © 2011-2022 走看看