PDF在線預覽 ,兼容IE,chrome。使用了pdfobject.js

PDF在線預覽,兼容IE,CHROMEjavascript

http://files.cnblogs.com/w519/PDFViewSolution.rarcss

本人寫的PDF在線預覽DEMO使用了pdfobject.js和adobe Reader閱讀器。java

由於PDFobjcet.js不兼容IE瀏覽器,而,因此在chrome瀏覽器上pdfobjcet.js打開PDF,而IE瀏覽器中使用adobe Reader閱讀器打開PDF。jquery

問題:對於瀏覽器中設置了默認迅雷下載,當單擊網頁中a標籤連接時候,迅雷監測「連接」,帶有.pdf,.exe等結尾擴展名,默認爲下載,不能跳轉頁面。chrome

方法:能夠將url中文件擴展名去掉,就不會迅雷下載!瀏覽器

1.在Default.aspx頁面判斷瀏覽器,若是是IE,且判斷是否安裝了adobe Reader,跳轉到PDFView.aspx頁面,在後臺輸出PDF文件。若是是chrome就用pdfobject.js,在本頁面輸出PDFapp

Default.aspx代碼ide

  <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 = {
                        width:$(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代碼url

 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();
                    }
                }

        }

 

 

相關文章
相關標籤/搜索