PDFオンラインプレビュー、IE対応、chrome.pdfobjectを使用しました.js

19553 ワード

PDFオンラインプレビュー、IE対応、CHROME
http://files.cnblogs.com/w519/PDFViewSolution.rar
本人が書いたPDFオンラインプレビューDEMOはpdfobjectを使用しています.jsとadobe Readerリーダー.
PDFはjsはIEブラウザと互換性がないので、chromeブラウザ上でpdfobjcet.jsはPDFを開き、IEブラウザでadobe Readerリーダーを使用してPDFを開きます.
質問:ブラウザにデフォルトの迅雷ダウンロードが設定されている場合、Webページのaラベルリンクをクリックすると、迅雷モニタ「リンク」があります.pdf,.exeなどの末尾拡張子は、デフォルトでダウンロードされ、ページをジャンプできません.
方法:urlの中のファイルの拡張子を取り除くことができて、雷のダウンロードができません!
1.Default.aspxページはブラウザを判断する、IEであればadobe Readerがインストールされているか否かを判断し、PDF Viewにジャンプする.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 = {
                        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コード
 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();
                    }
                }

        }

 
 
PDF在线预览 ,兼容IE,chrome。使用了pdfobject.js_第1张图片 PDF在线预览 ,兼容IE,chrome。使用了pdfobject.js_第2张图片