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

服务端:
/// 
        /// 读取PDF文件
        /// 
        /// 文件名称(可以从其他地方传进来)
        /// 
        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");
        }

 

转载于:https://www.cnblogs.com/sophiel/p/9816087.html

你可能感兴趣的:(预览PDF文件(读取文件流方式))