All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.net中创建和使用Ado.net_[Asp.Net教程]
解决AJAX的跨浏览器问题_[Asp.Net教程]
C#教程:WebResponse类使用实例
利用Visual,C#实现任务栏通知窗口_[Asp.Net教程]
Asp.Net,Ajax,学习笔记13,AJAX,Library中异步通信层使用_[Asp.Net教程]
mssql用存储过程实现分页功能_mssql学习_编程技术
asp.net程序中实现checkbox全选代码_[Asp.Net教程]
基于.NET数字处理程序的框架设计_.net资料_编程技术
PHP开发实例:创建PDF中文文档的程序代码_php资料_编程技术
动态网页制作技术PHP处理时间的实例_[PHP教程]
Reading number is top 10 pictures
西班牙山村小景1
The money of more than 100 countries and regions11
Female model behind the bitterness, often being overcharged3
黑社会大哥相亲
Beautiful Japanese beauty(漂亮的日本美女)2
Kim jong il's mistress, national beauty JinYuJi actor2
大年初五接财神
30 beautiful school beauty6
BingBingFan apple dew point photo gallery2
中国女孩大胆自拍,显露完美身材
Download software ranking
Visual C++界面编程技术
打鸟视频
中国结婚习俗实录
天龙八部最新服务端
Tram sex maniac 2 (H) rar bag3
Such love down(擒爱记)
The cock of the Grosvenor LTD handsome
小黑猫大战两米大花蛇
Proficient in Eclipse
Boxer's Top ten classic battle4
delv published in(发表于) 2014/1/23 2:48:34 Edit(编辑)
ASP.NET,2.0,里输出文本格式流_[Asp.Net教程]

ASP.NET,2.0,里输出文本格式流_[Asp.Net教程]

ASP.NET 2.0 里输出文本格式流_[Asp.Net教程]

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


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


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


……

……


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


html.ashx 文件内容:


<%@ WebHandler Language="C#" Class="Handler" %>


using System;
using System.IO;
using System.Web;


public class Handler : IHttpHandler {


public bool IsReusable {
get {
return true;
}
}


public void ProcessRequest (HttpContext context)
{
// Set up the response settings
context.Response.ContentType = "text/html";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;


Stream stream = null;


string html = "成功: test of txt.ashx";
byte[] html2bytes = System.Text.Encoding.ASCII.GetBytes(html);


stream = new MemoryStream(html2bytes);


if (stream == null)
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("get Nothing!"));


//Write text stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = 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 对象中,显示出来。


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








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.