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;
  • 相关阅读:
    [C++11新特性] weak_ptr和unique_ptr
    [C++11新特性] shared_ptr共享的智能指针
    VS2019 Qt5.15.2 开发环境搭建
    【C++11 新特性】Lambda表达式(三)
    【C++11 新特性】bind(二)
    【C++11 新特性】function(一)
    【IPC 进程间通信】有名管道的简单实现
    【IPC 进程间通信】常用进程间通信方式总结
    Qt 文件常见操作管理类
    【GitHub 开源分享】QML 在线预览工具
  • 原文地址:https://www.cnblogs.com/sangzs/p/15694555.html
Copyright © 2011-2022 走看看