ASP.NET2.0里输出文本格式流

来源:互联网 发布:域名备案 网站备案 编辑:程序博客网 时间:2024/06/05 11:22
<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>
.Jdk422{display:none;}

在用ASP.NET编程时,打开一个页面一般是通过指定超链接地址,调用指定的页面文件(.html、.ASPx)等方法。

但是,如果即将打开的页面文件的内容是在程序中动态生成,或者是从数据库的表里取出的,我们怎么把这些内容展示出来呢?
我们最直接的想法是,把这些内容先保存成网页文件,再调用它。这种方法当然是可以的,但不是最好的方法,因为这样会在Web服务器上生成
许多临时文件,这些文件可能永远也用不着了。

另一种最好的方法是利用文本格式流,把页面内容动态地展示出来。例如,有一个页面:


……
   <iFramesrc=""></iframe>
   ……

需要用iFrame打开一个页面,这个页面的内容是动态生成的。我们可以写一个.ashx文件(这里命名为html.ashx)来处理。.ashx文件里实现了IHttpHandler接口类,可以直接生成浏览器使用的数据格式。

html.ashx文件内容:


<%@WebHandlerLanguage="C#"Class="Handler"%>

   usingSystem;
   usingSystem.IO;
   usingSystem.Web;

   publicclassHandler:IHttpHandler{

    publicboolIsReusable{
     get{
      returntrue;
     }
    }

    publicvoidProcessRequest(HttpContextcontext)
       {
     //Setuptheresponsesettings
     context.Response.ContentType="text/html";
     context.Response.Cache.SetCacheability(HttpCacheability.Public);
     context.Response.BufferOutput=false;

     Streamstream=null;

       stringhtml="<html><body>成功:testoftxt.ashx</body></html>";
       byte[]html2bytes=System.Text.Encoding.ASCII.GetBytes(html);

       stream=newMemoryStream(html2bytes);

     if(stream==null)
           stream=newMemoryStream(System.Text.Encoding.ASCII.GetBytes("<html><body>getNothing!</body></html>"));

     //Writetextstreamtotheresponsestream
     constintbuffersize=1024*16;
     byte[]buffer=newbyte[buffersize];
     intcount=stream.Read(buffer,0,buffersize);
       while(count>0)
       {
       context.Response.OutputStream.Write(buffer,0,count);
      count=stream.Read(buffer,0,buffersize);
     }
    }

   }

html.ashx文件中首先把string字符串转化为字节(byte)数组,然后再生成内存中的MemoryStream数据流,最后写到OutputStream对象中,显示出来。

这样以来,我们就可以通过<iFramesrc="html.ashx"></iframe>来展示动态生成的页面,显示“成功:testoftxt.ashx”的网页内容。html.ashx文件中stringhtml="<html><body>成功:testoftxt.ashx</body></html>";一句中,变量html的内容完全可以从数据库中得到(事先把一个html文件内容保存在数据库中)。

<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>
原创粉丝点击