给你我的理解吧:
HttpResponse.Flush()方法
将当前缓冲的所有输出强制发送到客户端。 在请求处理期间,可以多次调用 Flush 方法。实例:
下面的示例调用 Save 方法将一个 Bitmap 对象保存到 OutputStream 属性中,并将图像转换为 JPEG 格式。然后,代码调用 Bitmap 对象和 Graphics 对象的 Dispose 方法,释放它们正在使用的资源。最后,该代码调用 Flush 方法将响应的内容发送到请求客户端。- <%@ Page Language="C#" %>
- <%@ import Namespace="System.Drawing" %>
- <%@ import Namespace="System.Drawing.Imaging" %>
- <%@ import Namespace="System.Drawing.Drawing2D" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <script runat="server">
- private void Page_Load(object sender, EventArgs e)
- {
- // Set the page's content type to JPEG files
- // and clear all response headers.
- Response.ContentType = "image/jpeg";
- Response.Clear();
- // Buffer response so that page is sent
- // after processing is complete.
- Response.BufferOutput = true;
- // Create a font style.
- Font rectangleFont = new Font(
- "Arial", 10, FontStyle.Bold);
- // Create integer variables.
- int height = 100;
- int width = 200;
- // Create a random number generator and create
- // variable values based on it.
- Random r = new Random();
- int x = r.Next(75);
- int a = r.Next(155);
- int x1 = r.Next(100);
- // Create a bitmap and use it to create a
- // Graphics object.
- Bitmap bmp = new Bitmap(
- width, height, PixelFormat.Format24bppRgb);
- Graphics g = Graphics.FromImage(bmp);
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.Clear(Color.LightGray);
- // Use the Graphics object to draw three rectangles.
- g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
- g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
- g.DrawRectangle(Pens.Black, 0, 0, width, height);
- // Use the Graphics object to write a string
- // on the rectangles.
- g.DrawString(
- "ASP.NET Samples", rectangleFont,
- SystemBrushes.WindowText, new PointF(10, 40));
- // Apply color to two of the rectangles.
- g.FillRectangle(
- new SolidBrush(
- Color.FromArgb(a, 255, 128, 255)),
- x, 20, 100, 50);
- g.FillRectangle(
- new LinearGradientBrush(
- new Point(x, 10),
- new Point(x1 + 75, 50 + 30),
- Color.FromArgb(128, 0, 0, 128),
- Color.FromArgb(255, 255, 255, 240)),
- x1, 50, 75, 30);
- // Save the bitmap to the response stream and
- // convert it to JPEG format.
- bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
- // Release memory used by the Graphics object
- // and the bitmap.
- g.Dispose();
- bmp.Dispose();
- // Send the output to the client.
- Response.Flush();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>ASP.NET Example</title>
- </head>
- <body>
- <form id="form1" runat="server">
- </form>
- </body>
- </html>
复制代码 HttpResponse中的End()方法
將目前所有受緩衝的輸出傳送到用戶端、停止網頁的執行,並引發 EndRequest 事件。下面的示例使用 IsClientConnected 属性来检查请求页的客户端是否仍与服务器连接。如果 IsClientConnected 为 true,代码将调用 Redirect 方法,因此客户端将可以查看另一页。如果 IsClientConnected 为 false,代码将调用 End 方法,并且所有页处理都将终止。- [code]<%@ Page Language="C#" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <script runat="server">
- private void Page_Load(object sender, EventArgs e)
- {
- // Check whether the browser remains
- // connected to the server.
- if (Response.IsClientConnected)
- {
- // If still connected, redirect
- // to another page.
- Response.Redirect("Page2CS.aspx", false);
- }
- else
- {
- // If the browser is not connected
- // stop all response processing.
- Response.End();
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>ASP.NET Example</title>
- </head>
- <body>
- <form id="form1" runat="server">
- </form>
- </body>
- </html>
复制代码 [/code]
|