自訂HTTP處理常式顯示圖片(asp.net 2.0)

來源:互聯網
上載者:User

自訂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;
    }
   }
  }
 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.