mvc在頁面上顯示PDF

/// <summary>
        /// 讀取PDF文件
        /// </summary>
        /// <param name="fName">文件名稱(能夠從其餘地方傳進來)</param>
        /// <returns></returns>
        public FileStreamResult readPDF(string fName = "隨便.pdf")
        {
            string dirp = @"放pdf文件的文件夾路徑";
            DirectoryInfo mydir = new DirectoryInfo(dirp);
            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 == fName)
                    {
                        pdfSrc = dirp + "\\" + fi.Name;//pdf路徑
                          ViewBag.title = fi.Name;//網頁標題
                      }
                }
            }
            FileStream fs = new FileStream(pdfSrc, FileMode.Open, FileAccess.Read);
            return File(fs, "application/pdf");
        }
相關文章
相關標籤/搜索