查看文章
 
asp.net 里的HttpHandler隐藏图片
2008年11月28日 星期五 16:14

namespace CustomHander
{
   
/// <summary>
   
/// CustomHandler 的摘要说明
   
/// </summary>
    public class ImageHandler : IHttpHandler
    {
       
public bool IsReusable
        {
           
get { return false; }
        }
       
public void ProcessRequest(HttpContext context)
        {
           
if (!string.IsNullOrEmpty(context.Request.QueryString["picture"]))
            {
               
string fileName = context.Request.QueryString["picture"];
                OnServing(fileName);
               
try
                {
                   
string folder = "App_Data/Picture/";//图片路径请自己定义
                    FileInfo fi
= new FileInfo(context.Server.MapPath(folder) + fileName);

                   
if (fi.Exists &&fi.Directory.FullName.ToUpperInvariant().Contains (Path.DirectorySeparatorChar + "PICTURE"))
                    {
                       
//context.Response.Cache.SetCacheability(HttpCacheability.Public);
                       
//context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));

                       
//if (Utils.SetConditionalGetHeaders(fi.CreationTimeUtc))
                       
//    return;

                       
int index = fileName.LastIndexOf(".") + 1;
                       
string extension = fileName.Substring(index).ToUpperInvariant();

                       
// Fix for IE not handling jpg image types
                        if (string.Compare(extension, "JPG") == 0)
                            context.Response.ContentType
= "image/jpeg";
                       
else
                            context.Response.ContentType
= "image/" + extension;

                        context.Response.TransmitFile(fi.FullName);
                        OnServed(fileName);
                    }
                   
else
                    {
                        OnBadRequest(fileName);
                        context.Response.ContentType
= "image/gif";
                        context.Response.TransmitFile(context.Server.MapPath(folder)
+ "nophoto.gif");
                       
//context.Response.Redirect(Utils.AbsoluteWebRoot + "error404.aspx");
                    }
                }
               
catch (Exception ex)
                {
                    OnBadRequest(ex.Message);
                   
//context.Response.Redirect(Utils.AbsoluteWebRoot + "error404.aspx");
                }
            }
        }

       
/**/
       
/// <summary>
       
/// Occurs before the requested image is served.
       
/// </summary>
        public static event EventHandler<EventArgs> Serving;
       
private static void OnServing(string file)
        {
           
if (Serving != null)
            {
                Serving(file, EventArgs.Empty);
            }
        }

       
/**/
       
/// <summary>
       
/// Occurs when a file is served.
       
/// </summary>
        public static event EventHandler<EventArgs> Served;
       
private static void OnServed(string file)
        {
           
if (Served != null)
            {
                Served(file, EventArgs.Empty);
            }
        }

       
/**/
       
/// <summary>
       
/// Occurs when the requested file does not exist.
       
/// </summary>
        public static event EventHandler<EventArgs> BadRequest;
       
private static void OnBadRequest(string file)
        {
           
if (BadRequest != null)
            {
                BadRequest(file, EventArgs.Empty);
            }
        }
    }
}

<configuration>
   
<appSettings/>
   
<connectionStrings/>
   
<system.web>
       
<httpHandlers>
           
<add verb="*" path="image.axd" type="CustomHandler.ImageHandler" validate="true"/>
       
</httpHandlers>
   
</system.web>
</configuration>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   
<title>无标题页</title>
</head>
<body>
   
<form id="form1" runat="server">
   
<div>
       
<img src="/image.axd?picture=IP.jpg" /></div>
   
</form>
</body>
</html>


类别:asp.net||添加到搜藏 |分享到i贴吧|浏览(1194)|评论 (0)
 
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu