1. 程式人生 > >ASP 在網頁中 開啟PDF檔案

ASP 在網頁中 開啟PDF檔案

傳送URL:

 if (File.Exists(p_strUrl))
            {  //Server.UrlEncode(path)
                this.Page.RegisterStartupScript("", "<script>window.open('XXX.aspx?pdfPath=" + Server.UrlEncode(p_strUrl) + "')</script>");
                return;
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('未找到PDF檔案!')</script>");
                return;
            }

跳轉的介面(XXX.aspx?)就收URL:

            pdfPath = Server.UrlDecode(Request["pdfPath"].ToString());//獲取傳來的路徑
            try
            {
                FileStream fs = File.Open(pdfPath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
                if (fs.Length > 0)
                {
                    byte[] bfBuf = new byte[fs.Length];
                    bfBuf.Initialize();
                    fs.Read(bfBuf, 0, (int)fs.Length);
                    Response.ContentType = "application/PDF";
                    Response.OutputStream.Write(bfBuf, 0, bfBuf.Length);//以流的形式顯示在網頁上
                    // Response.ContentType =
                    // Response.BinaryWrite(bfBuf);
                    Response.End();
                    fs.Close();
                }
            }
            finally
            {
                ;
            }