HttpResponse.Flush 方法

      假如用户请求一个页面,而这个页面在后端服务器需要花200至500毫秒乃至更长时间才能生成最终HTML页面,这时候用户浏览器处于较长时间的、等待页面数据返回的空闲状态,用户体验不会很好。此时可以根据页面内容长短做适当分隔,将先生成的页面局部HTML缓冲内容提前发送到客户端,不必让服务器消耗内存缓冲完整个庞大的页面内容后再行输出。这种方法有益于处理后端负荷大而前端负荷轻的页面。
    在HTML页面的head标签位置后是清除缓冲的好位置,因为HTML的head标签可以包括 CSS 和 JavaScript 文件,对于浏览器而言获取页面显示与后端服务器处理并行的效果较好。

<%@ 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)
    {
        //String fileName = Server.MapPath("/img/pic .jpg");
        //Response.ContentType = "image/jpeg";
        //Response.WriteFile(fileName);
 
        // 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>

注意:在实际Web开发中,尽量减少HTTP请求次数是优化的重要方面,但是使用尽早清除缓冲语句会增加一个页面的HTTP请求次数,这无疑是一个矛盾,因此请注意适用范围,不要滥用它。

posted @ 2008-12-18 12:59  Icyflash  阅读(1037)  评论(0编辑  收藏  举报