自定义HTTP处理程序显示图片(asp.net 2.0)
2006-11-21 13:29
489 查看
自定义HTTP处理程序,就是HTTP处理程序必须实现接口System.Web.IHttpHandler。
在实现接口IHttpHandler时,必须做两件事,一件事是设置IsReusable属性,另一件事是实现ProcessRequest()方法。
创建Generic Handler.
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.IO;
using System.Web;
public class Handler : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest (HttpContext context) {
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// Setup the Size Parameter
PhotoSize size;
switch (context.Request.QueryString["Size"]) {
case "S":
size = PhotoSize.Small;
break;
case "M":
size = PhotoSize.Medium;
break;
case "L":
size = PhotoSize.Large;
break;
default:
size = PhotoSize.Original;
break;
}
// Setup the PhotoID Parameter
Int32 id = -1;
Stream stream = null;
if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "") {
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
stream = PhotoManager.GetPhoto(id, size);
} else {
id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
stream = PhotoManager.GetFirstPhoto(id, size);
}
// Get the photo from the database, if nothing is returned, get the default "placeholder" photo
if (stream == null) stream = PhotoManager.GetPhoto(size);
// Write image stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
}
public static Stream GetPhoto(int photoid, PhotoSize size) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("GetPhoto", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@PhotoID", photoid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
} catch {
return null;
}
}
}
}
public static Stream GetFirstPhoto(int albumid, PhotoSize size) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("GetFirstPhoto", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", albumid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
} catch {
return null;
}
}
}
}
在实现接口IHttpHandler时,必须做两件事,一件事是设置IsReusable属性,另一件事是实现ProcessRequest()方法。
创建Generic Handler.
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.IO;
using System.Web;
public class Handler : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest (HttpContext context) {
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// Setup the Size Parameter
PhotoSize size;
switch (context.Request.QueryString["Size"]) {
case "S":
size = PhotoSize.Small;
break;
case "M":
size = PhotoSize.Medium;
break;
case "L":
size = PhotoSize.Large;
break;
default:
size = PhotoSize.Original;
break;
}
// Setup the PhotoID Parameter
Int32 id = -1;
Stream stream = null;
if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "") {
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
stream = PhotoManager.GetPhoto(id, size);
} else {
id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
stream = PhotoManager.GetFirstPhoto(id, size);
}
// Get the photo from the database, if nothing is returned, get the default "placeholder" photo
if (stream == null) stream = PhotoManager.GetPhoto(size);
// Write image stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
}
public static Stream GetPhoto(int photoid, PhotoSize size) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("GetPhoto", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@PhotoID", photoid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
} catch {
return null;
}
}
}
}
public static Stream GetFirstPhoto(int albumid, PhotoSize size) {
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString)) {
using (SqlCommand command = new SqlCommand("GetFirstPhoto", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", albumid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
} catch {
return null;
}
}
}
}
相关文章推荐
- ASP.NET 之 自定义 异步HTTP处理程序
- ASP.NET 3.5核心编程学习笔记(43):HTTP处理程序的编写之图片查看器
- ASP.NET:在一般处理程序中通过 Session 保存验证码却无法显示图片?
- ASP.NET之自定义同步HTTP处理程序(图文教程)
- asp.net利用自定义Http处理程序实现URL伪静态之初探
- ASP.NET之自定义异步HTTP处理程序(图文教程)
- ASP.NET之自定义同步HTTP处理程序(图文教程)
- asp.net利用自定义Http处理程序实现URL伪静态之初探
- ASP.net的HTTP处理程序(ASHX)提取dll内嵌资源图片
- ASP.NET 之 自定义 异步HTTP处理程序
- ASP.NET之自定义异步HTTP处理程序(图文教程)
- ASP.NET 之 自定义 同步HTTP处理程序
- ASP.NET之自定义异步HTTP处理程序(图文教程)
- ASP.Net中自定义Http处理及应用之HttpModule篇
- 解决asp.net Sharepoint无法连接发布自定义字符串处理程序,不能进行输出缓存处理的方法
- ASP.NET(IIS)出现"没有为请求类型"GET"找到 HTTP 处理程序"
- URL重写及ASP.NET路由、Http处理模块、程序等
- ASP.Net中自定义Http处理及应用之HttpModule篇 (转载)
- asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现
- ASP.NET 3.5核心编程学习笔记(45):HTTP处理程序之高级编程