使用.NET 向浏览器写入二进制文件

来源:互联网 发布:加诺格淘宝网店真的吗 编辑:程序博客网 时间:2024/05/16 09:16
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1、确保要从其上查看 .aspx 页的客户端计算机上已安装了 Adobe Acrobat Reader,以便浏览器能够正确读取并呈现二进制数据。可以从以下 Adobe 网站下载 Adobe Acrobat Reader:

http://www.chinese-s.adobe.com/main.html (http://www.adobe.com)
2、确保将您的页面添加到项目中在上一节中添加的 .pdf 文件所在的级别。这一点非常重要,因为代码最初引用 .pdf 文件时采用相对路径。

在代码隐藏页的 Page_Load 事件中,单击“编辑”菜单上的“粘贴”以粘贴代码:

                   

  在代码隐藏页的 Page_Load 事件中,加入以下代码:
private void Page_Load(object sender, System.EventArgs e)
    ...{
             //Set the appropriate ContentType.
        Response.ContentType = "Application/pdf";
             //Get the physical path to the file.
        string FilePath = MapPath("acrobat.pdf");
             //Write the file directly to the HTTP content output stream.
        Response.WriteFile(FilePath);
            Response.End();
    }
                     <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>