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;
  • 相关阅读:
    jquery学习
    java--MVC引入JUnit单元测试
    BAE引擎发布到外网
    ORACLE1.26 综合:游标和动态SQL
    ORACLE1.25 动态SQL
    ORACLE1.24 银行系统操作和游标
    ORACLE1.23 loop,whild.for循环
    ORACLE1.23 if case when
    ORACLE1.22 %type %rowtype
    ORACLE1.21 PLSQL 01
  • 原文地址:https://www.cnblogs.com/sangzs/p/15694555.html
Copyright © 2011-2022 走看看