ASP 在网页中 打开PDF文件

本文介绍了一种在ASP.NET应用中通过客户端JavaScript安全打开PDF文件的方法,并详细展示了如何在服务器端验证文件存在并正确读取PDF内容,确保用户在浏览器中能够顺利查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发送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
            {
                ;
            }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值