<1>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ASP.NET中级
{
/// <summary>
/// flush 的摘要说明
/// </summary>
public class flush : IHttpHandler
{
//(位图图像就是由一个一个像素组成的)
public void ProcessRequest(HttpContext context)
{
//------------------------Response.ContentType--输出流的内容类型---------------------------
context.Response.ContentType = "text/html";
//----------------------------Response.Flush()----Response.Clear()------处理缓存区数据--------------------------
for (int i = 0; i < 200; i++)
{
if (i >= 100)
{
context.Response.Clear(); //当i=100的时候清空缓存区的数据,不给它发送浏览器,直接清空
}
context.Response.Write("第" + i + "步已经完成<br/>");
context.Response.Flush(); //将缓存区的数据立即发送给浏览器。注:这一步应该放到Write后面
}
//-------------------------------Response.ContentEncoding------设置输出流的编码----------------
// context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流的编码为utf-8
//--------------------------------Drawing.Bitmap-----创建位图对象-----------------------------
context.Response.ContentType="text/JPEG";
//OutputStream输出二进制流的使用(图片,Excel文件等非文本输出),把二进制的数据输出到客户端
string fullpath = context.Request.MapPath("~/images/123.jpg");
//创建一个位图对象(画布);从指定的现有图像初始化 System.Drawing.Bitmap 类的新实例
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
{
//将此图像以指定的格式(Jpeg)保存到指定的流中
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//一般处理图片啊,Excel文件啊,一般都用这种outputStream这种二进制流的形式来输出。
}
//------------------------Response.Buffer---是否启用缓存----------------------------------
//Response.Buffer与Response.BufferOutput是一模一样的,建议使用Response.BufferOutput
//buffer的使用,是否启用响应缓存 默认值为true
//如果启用了响应缓存机制,就输出“你好”,没有启用,就输出图片
if (context.Response.Buffer == false)
{
context.Response.ContentType = "image/JPEG";
string fullpath1 = context.Request.MapPath("/images/123.jpg");
//创建一个位图对象(画布),并用指定的图片初始化它
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath1))
{
//将此图像以指定的格式(Jpeg)保存到指定的流中
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("你好");
}
//------------------------------Response.End()---终止响应-----------------------------------
//End终止响应的使用
context.Response.ContentType = "text/plain";
context.Response.Write("终止响应前");
context.Response.End();//终止响应,不再往下面执行了
context.Response.Write("这一步将不会被执行到,所以无法输出");
//---------------------Response.Redirect--------Server.Transfer-------网页重定向------------
//Redirect("a.html")是向浏览器发回302重定向,是通知浏览器重新访问URL这个网址,这个过程经历了服务器通知浏览器重新访问URL这个网址,及浏览器接到命令后访问新网址的过程(后面会用来防止刷新浏览器时提示"重试")
context.Response.Redirect("/images/123.jpg");//跳转到图片
context.Response.Redirect("http://www.baidu.com");//跳转到网页
//Response对象的Redirect与Server对象的Transfer对比的区别: context.Server.Transfer("~/About.aspx");//内部重定向请求。context.Server.Transfer("~/About.aspx")将用户请求重定向给~/About.aspx处理,是服务器内部的接管,浏览器是意识不到这个接管的(所以HTTP响应报文状态码是200 OK 而不是302 重定向),因此浏览器地址栏不会像Response.Redirect("~/About.aspx")那样经过两次响应,它一次响应就够了(是一次HTTP请求)。所以浏览器地址栏并不会发生变化(注意Server.Transfer()是服务器内部接管,因此它不能像Response.Redirect()那样定义到外部网站)
//------------------------Response.WriteFile()-----向浏览器输出文件---------------------------
//context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/images/123.jpg");//向浏览器输出文件,这个方法很少用,知道就行了。因为大文件输出的话是很耗费资源的。如果你不做处理的话,你直接让客户下载文件不就可以了吗?没必要用把文件输出到浏览器。在这里为输出一张名为女人的JPG图片
context.Response.ContentType = "text/plain";
//context.Response.WriteFile("d:/123.txt"); // 将d盘下的123.txt文件输出到浏览器
}
public bool IsReusable
{
get
{
return false;
}
}
}
}