C# MVC PDFJS URL传参方式+文件流方式在线展示文档

一、服务端返回PDF文件流

[HttpGet, Route("GetMediaByPdfFile"),]
        public HttpResponseMessage GetMediaByPdfFile(string projectCode, string fileId, string fileName)
        {
            HttpResponseMessage httpResponseMessage = null;
            try
            {
                if (string.IsNullOrWhiteSpace(projectCode) || string.IsNullOrWhiteSpace(fileId) || string.IsNullOrWhiteSpace(fileName))
                {
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("缺少参数") };
                }
                var pf = bimFaceService.GetProjectFileById(fileId);
                if (pf == null)
                {
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("未找到此文件") };
                }
                string localFilePath = Path.Combine(new VaultFactory().GetBaseDirectory(), projectCode, pf.Id + Path.GetExtension(pf.FileName));
                if (!File.Exists(localFilePath))
                {
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("未找到此文件") };
                }
                string localFileName = Path.GetFileName(localFilePath);
                var fileStream = new VaultFactory().GetVault(projectCode).GetStream(localFileName);
                if (fileStream == null)
                {
                    return new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("未找到此文件") };
                }
                var browser = string.Empty;
                if (Request.Headers.UserAgent != null)
                {
                    browser = Request.Headers.UserAgent.ToString().ToUpper();
                }
                httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                httpResponseMessage.Content = new StreamContent(fileStream);
                httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
                {
                    FileName =
                    browser.Contains("CHROME")
                        ? fileName
                        : HttpUtility.UrlEncode(fileName)
                };
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(ex.Message, ex);
                httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }
            return httpResponseMessage;
        }

二、手动解析URL编码

// 服务器控制器接收参数(手工解析)
public string GetParamsByUrlQuery(string urlQuery,string key)
        {
            string value = "";
            try
            {
                string str1 = urlQuery.Substring(urlQuery.LastIndexOf('?') + 1);
                string[] str2 = str1.Split('&');
                foreach (var item in str2)
                {
                    string k = item.Split('=')[0];
                    string v = item.Split('=')[1];
                    if (k.ToLower() == key.ToLower())
                    {
                        value = v;
                        break;
                    }
                }
                return value;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString(), ex);
                return value;
            }
        }
 
...
string projectCode = Request.QueryString["ProjectCode"] ?? GetParamsByUrlQuery(urlQuery, "ProjectCode"); string fileId = Request.QueryString["FileId"] ?? GetParamsByUrlQuery(urlQuery, "FileId"); string fileName = Request.QueryString["FileName"] ?? GetParamsByUrlQuery(urlQuery, "FileName");

...

三、前端展示

        let filePath = `${window.location.origin}/api/bimface/GetMediaByPdfFile?projectCode=${obj.ProjectCode}&fileId=${obj.FileId}&fileName=${obj.FileName}`;
        filePath = encodeURIComponent(filePath);  // 重要:此编码动作用于防止PDFJS内部提供的URL提取参数方法解析出现歧义的BUG
        let fullPath = `/Content/plugins/preview/pdfjs-2.8.335-dist/web/viewer.html?file=${filePath}`;
        document.querySelector("#pdfFrame").src = fullPath;

 

posted @ 2021-12-15 18:46  最好的年纪  阅读(585)  评论(0编辑  收藏  举报