欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net core PDF下载接口

程序员文章站 2024-01-21 15:19:16
...

PDF下载接口

  1. Controller层
 [Area("Api")]
    public class DownloadPDFController : Controller
    {
        public readonly IWebHostEnvironment _webHostEnvironment;
        public DownloadPDFController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

        public ActionResult DownloadFile(int goodID,string openID)
        {
            IH5Service m_H5Service = IocProxyFactory.GetService<IH5Service>();
            try
            {
                //获取存储静态资源的wwwroot所在的根目录
                string webRootPath = _webHostEnvironment.WebRootPath;

                var reportInfo = m_H5Service.GetReportInfo(goodID,openID);

                var path = reportInfo.Replace("/", "\\");

                var fileName = webRootPath + path;

                LogHelper.Info("pdf路径:" + fileName);

                if (reportInfo != null)
                {
                    var contentType = "application/msword";

                    if (fileName.ToLower().Contains(".pdf"))
                    {
                        contentType = "application/pdf";

                    }
                    else if (fileName.ToLower().Contains(".jpg"))
                    {
                        contentType = "image/jpeg";
                    }
                    else if (fileName.ToLower().Contains(".png"))
                    {
                        contentType = "image/png";
                    }

                    return File(reportInfo, contentType);

                }
                else
                {
                    return Json("信息不存在");

                }
            }
            catch (Exception ex)
            {
                LogHelper.Info("PDF_Error:" + ex.ToString());

                return null;
            }
        }


    }

2.service层
返回PDF文件路径

相关标签: C#