img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S"
很奇怪的事情,只是发现了,但不明白为什么!

 Handler
Handler
1 <%@ WebHandler Language="C#" Class="Handler" %>
<%@ WebHandler Language="C#" Class="Handler" %>
2
3 using System;
using System;
4 using System.IO;
using System.IO;
5 using System.Web;
using System.Web;
6
7
 public class Handler : IHttpHandler
public class Handler : IHttpHandler  {
{
8
9
 public bool IsReusable
    public bool IsReusable  {
{
10
 get
        get  {
{
11 return true;
            return true;
12 }
        }
13 }
    }
14 
    
15
 public void ProcessRequest (HttpContext context)
    public void ProcessRequest (HttpContext context)  {
{
16 // Set up the response settings
        // Set up the response settings
17 context.Response.ContentType = "image/jpeg";
        context.Response.ContentType = "image/jpeg";
18 context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
19 context.Response.BufferOutput = false;
        context.Response.BufferOutput = false;
20 // Setup the Size Parameter
        // Setup the Size Parameter
21 PhotoSize size;
        PhotoSize size;
22
 switch (context.Request.QueryString["Size"])
        switch (context.Request.QueryString["Size"])  {
{
23 case "S":
            case "S":
24 size = PhotoSize.Small;
                size = PhotoSize.Small;
25 break;
                break;
26 case "M":
            case "M":
27 size = PhotoSize.Medium;
                size = PhotoSize.Medium;
28 break;
                break;
29 case "L":
            case "L":
30 size = PhotoSize.Large;
                size = PhotoSize.Large;
31 break;
                break;
32 default:
            default:
33 size = PhotoSize.Original;
                size = PhotoSize.Original;
34 break;
                break;
35 }
        } 
36 // Setup the PhotoID Parameter
        // Setup the PhotoID Parameter
37 Int32 id = -1;
        Int32 id = -1;
38 Stream stream = null;
        Stream stream = null;
39
 if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
        if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")  {
{
40 id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
            id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
41 stream = PhotoManager.GetPhoto(id, size);
            stream = PhotoManager.GetPhoto(id, size);
42
 } else
        } else  {
{
43 id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
            id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
44 stream = PhotoManager.GetFirstPhoto(id, size);
            stream = PhotoManager.GetFirstPhoto(id, size);
45 }
        }
46 // Get the photo from the database, if nothing is returned, get the default "placeholder" photo
        // Get the photo from the database, if nothing is returned, get the default "placeholder" photo
47 if (stream == null) stream = PhotoManager.GetPhoto(size);
        if (stream == null) stream = PhotoManager.GetPhoto(size);
48 // Write image stream to the response stream
        // Write image stream to the response stream
49 const int buffersize = 1024 * 16;
        const int buffersize = 1024 * 16;
50 byte[] buffer = new byte[buffersize];
        byte[] buffer = new byte[buffersize];
51 int count = stream.Read(buffer, 0, buffersize);
        int count = stream.Read(buffer, 0, buffersize);
52
 while (count > 0)
        while (count > 0)  {
{
53 context.Response.OutputStream.Write(buffer, 0, count);
            context.Response.OutputStream.Write(buffer, 0, count);
54 count = stream.Read(buffer, 0, buffersize);
            count = stream.Read(buffer, 0, buffersize);
55 }
        }
56 }
    }
57
58 }
据说是能以流的方式直接读取存在数据库中的图片。
}
据说是能以流的方式直接读取存在数据库中的图片。 

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

3
 using System;
using System;4
 using System.IO;
using System.IO;5
 using System.Web;
using System.Web;6

7

 public class Handler : IHttpHandler
public class Handler : IHttpHandler  {
{8

9

 public bool IsReusable
    public bool IsReusable  {
{10

 get
        get  {
{11
 return true;
            return true;12
 }
        }13
 }
    }14
 
    15

 public void ProcessRequest (HttpContext context)
    public void ProcessRequest (HttpContext context)  {
{16
 // Set up the response settings
        // Set up the response settings17
 context.Response.ContentType = "image/jpeg";
        context.Response.ContentType = "image/jpeg";18
 context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Cache.SetCacheability(HttpCacheability.Public);19
 context.Response.BufferOutput = false;
        context.Response.BufferOutput = false;20
 // Setup the Size Parameter
        // Setup the Size Parameter21
 PhotoSize size;
        PhotoSize size;22

 switch (context.Request.QueryString["Size"])
        switch (context.Request.QueryString["Size"])  {
{23
 case "S":
            case "S":24
 size = PhotoSize.Small;
                size = PhotoSize.Small;25
 break;
                break;26
 case "M":
            case "M":27
 size = PhotoSize.Medium;
                size = PhotoSize.Medium;28
 break;
                break;29
 case "L":
            case "L":30
 size = PhotoSize.Large;
                size = PhotoSize.Large;31
 break;
                break;32
 default:
            default:33
 size = PhotoSize.Original;
                size = PhotoSize.Original;34
 break;
                break;35
 }
        } 36
 // Setup the PhotoID Parameter
        // Setup the PhotoID Parameter37
 Int32 id = -1;
        Int32 id = -1;38
 Stream stream = null;
        Stream stream = null;39

 if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
        if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")  {
{40
 id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
            id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);41
 stream = PhotoManager.GetPhoto(id, size);
            stream = PhotoManager.GetPhoto(id, size);42

 } else
        } else  {
{43
 id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
            id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);44
 stream = PhotoManager.GetFirstPhoto(id, size);
            stream = PhotoManager.GetFirstPhoto(id, size);45
 }
        }46
 // Get the photo from the database, if nothing is returned, get the default "placeholder" photo
        // Get the photo from the database, if nothing is returned, get the default "placeholder" photo47
 if (stream == null) stream = PhotoManager.GetPhoto(size);
        if (stream == null) stream = PhotoManager.GetPhoto(size);48
 // Write image stream to the response stream
        // Write image stream to the response stream49
 const int buffersize = 1024 * 16;
        const int buffersize = 1024 * 16;50
 byte[] buffer = new byte[buffersize];
        byte[] buffer = new byte[buffersize];51
 int count = stream.Read(buffer, 0, buffersize);
        int count = stream.Read(buffer, 0, buffersize);52

 while (count > 0)
        while (count > 0)  {
{53
 context.Response.OutputStream.Write(buffer, 0, count);
            context.Response.OutputStream.Write(buffer, 0, count);54
 count = stream.Read(buffer, 0, buffersize);
            count = stream.Read(buffer, 0, buffersize);55
 }
        }56
 }
    }57

58
 }
}.jpg) 
  
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号