zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    如何动态确定命名空间
    五种提高 SQL 性能的方法
    无意间发现收藏夹的秘密(^_^,也许大家早就知道了?)
    每个开发人员现在应该下载的十种必备工具,这个是中文的哦
    一个让我狂晕的异常及例行xiao总结
    几个开源项目实体层实现方式比较
    线程池在web上的简单应用
    不为"事务"而"事务"
    构造函数,静态构造函数与继承链
    小Tips两则
  • 原文地址:https://www.cnblogs.com/sangzs/p/15694555.html
Copyright © 2011-2022 走看看