zoukankan      html  css  js  c++  java
  • PDF在线预览 ,兼容IE,chrome。使用了pdfobject.js

    PDF在线预览,兼容IE,CHROME

    https://files.cnblogs.com/w519/PDFViewSolution.rar

    本人写的PDF在线预览DEMO使用了pdfobject.js和adobe Reader阅读器。

    因为PDFobjcet.js不兼容IE浏览器,而,所以在chrome浏览器上pdfobjcet.js打开PDF,而IE浏览器中使用adobe Reader阅读器打开PDF。

    问题:对于浏览器中设置了默认迅雷下载,当单击网页中a标签链接时候,迅雷监测“链接”,带有.pdf,.exe等结尾扩展名,默认为下载,不能跳转页面。

    方法:可以将url中文件扩展名去掉,就不会迅雷下载!

    1.在Default.aspx页面判断浏览器,如果是IE,且判断是否安装了adobe Reader,跳转到PDFView.aspx页面,在后台输出PDF文件。如果是chrome就用pdfobject.js,在本页面输出PDF

    Default.aspx代码

      <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script src="Scripts/pdfobject.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                var w = $(document).width();
                var h = $(document).height();            
                $("#pdf1").css("width", w).css("height", h);
                
    
                // 下面代码都是处理IE浏览器的情况 
                if (window.ActiveXObject || "ActiveXObject" in window) {
                    //判断是否为IE浏览器,"ActiveXObject" in window判断是否为IE11
                    //判断是否安装了adobe Reader
                    for (x = 2; x < 10; x++) {
                        try {
                            oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
                            if (oAcro) {
                                flag = true;
                            }
                        } catch (e) {
                            flag = false;
                        }
                    }
                    try {
                        oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
                        if (oAcro4) {
                            flag = true;
    
                        }
                    } catch (e) {
                        flag = false;
                    }
                    try {
                        oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
                        if (oAcro7) {
                            flag = true;
                        }
                    } catch (e) {
                        flag = false;
                    }
                    if (flag) {
                        $('#pdf1').hide();
                        location = "PDFView.aspx";
                    }
                    else {
                        alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装!");
                        location = "http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe";
                    }
                }
                else {
                    //alert($.trim(urls));
                    var success = new PDFObject({ url: 'PDF/19-P1012728-064_ZHCN-Draft.pdf', pdfOpenParams: { scrollbars: '0', toolbar: '0', statusbar: '0'} }).embed("pdf1");
                    if (!success) {
                        var opts = {
                            $(document).width(),
                            height: $(document).height(),
                            autoplay: true
                        };
                    }
                }
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">    
         <div id="pdf1" >对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装!
         <a href="PDF/19-P1012728-064_ZHCN-Draft.pdf">PDF/19-P1012728-064_ZHCN-Draft.pdf</a>
         </div>    
        </form>
    View Code

    PDFView.aspx.cs代码

     protected void Page_Load(object sender, EventArgs e)
            {
                if (Request.Browser.Browser.ToLower() != "chrome")
                {
                    
                        string filePath = Server.MapPath("pdf/19-P1012728-064_ZHCN-Draft.pdf");
                        Response.ClearContent();
                        Response.ClearHeaders();
                        string FilePost = filePath.Substring(filePath.Length - 3).ToLower();
                        switch (FilePost)
                        {
                            case "pdf":
                                Response.ContentType = "application/PDF";
                                break;
                            case "doc":
                                Response.ContentType = "application/msword";
                                break;
                            case "xls":
                                Response.ContentType = "application/vnd.ms-excel";
                                break;
                            default:
                                Session["ErrorInfo"] = "不支持的文件格式:" + FilePost;
                                Response.Redirect("ErrorPage.aspx");
                                break;
                        }
                        Response.WriteFile(filePath);
                        Response.Flush();
                        Response.Close();
                        Session.Remove("Report");
                    
                }
                else if (Request.Browser.Browser.ToLower() == "chrome")
                {
    
                    string filePath = Server.MapPath("pdf/19-P1012728-064_ZHCN-Draft.pdf");
                        Response.ClearContent();
                        Response.ClearHeaders();
                        string FilePost = filePath.Substring(filePath.Length - 3).ToLower();
                        Response.Clear();
                        Response.ClearHeaders();
                        Response.Buffer = false;
    
                        if (Request.Browser.Browser == "Firefox")
                            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "1.pdf");
                        else
                            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("1.pdf", System.Text.Encoding.UTF8));               
    
                        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open))
                        {
                            byte[] by = new byte[fs.Length];
                            fs.Read(by, 0, by.Length);
                            Response.BinaryWrite(by);
                            Response.AddHeader("Accept-Language", "zh-tw");
                            Response.ContentType = "application/octet-stream";
                            Response.AppendHeader("Content-Length ", by.Length.ToString());
                            System.Web.HttpContext.Current.Response.Flush();
                            System.Web.HttpContext.Current.Response.End();
                        }
                    }
    
            }

  • 相关阅读:
    多线程的创建方式
    ArrayList 初探
    java创建对象的几种方式
    select2动态查询及多选
    RabbitMQ 消息发送、消息监听
    tr命令
    集群,分布式,微服务概念和区别理解
    xargs命令
    shell中的EOF用法
    字段分隔符IFS
  • 原文地址:https://www.cnblogs.com/w519/p/4140100.html
Copyright © 2011-2022 走看看