PDF.JS 读取文件流前端展示 C#

最近再搞PDF得展示问题,因为aspose.pdf成本太高,只能使用pdf.js这个开源强大的前端东东了。

在百度了很久后 网上大都是node,java,php的事例,有位大哥的是C#的后台代码按他写的前端不生效,最终代码更改如下

public void GetDoc(string id)
        {           
            try
            {
                var model = zSWD_DocService.GetModelById(id);
                var path = System.Web.HttpContext.Current.Server.MapPath(model.DocFileUrl);
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {

                    FileInfo fi = new FileInfo(path);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.AppendHeader("Access-Control-Allow-Origin", "*");
                    Response.AppendHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(path);
                    Response.Flush();
                    Response.End();

                }


            }
            catch (Exception ex)
            {

            }
        }

  其中一篇文章可以参考 https://blog.csdn.net/u013132051/article/details/76066920

另我的项目中,没有像参考文档中加上http的 是可以直接虚路径的 Scripts/PDFJS/web/viewer.html?file=%2FZSWDManage%2Fdoc%2FGetDoc%3Fid%3D7237fe80-9762-4178-9244-7d05adfa47fd

其他的一些修改

1.中文显示问题 修改 <link rel="resource" type="application/l10n" href="locale/locale.properties"> 为  <link rel="resource" type="application/l10n" href="locale/locale.txt">

并注释

x.responseType = 'moz-chunked-arraybuffer';

2.默认url 修改为“”

 参考 http://www.bubuko.com/infodetail-2727204.html

实现效果

 

原文地址:https://www.cnblogs.com/DDSkay/p/11588943.html