Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Close window without confirm (I.E only) window.opener=null; window.open('','_self'); window.close(); focus on validating function focusOnInvalidControl() {     for (var i = 0; i < Page_Validators.length; i++) {         if (!Page_Validators[i].isvalid) {     ...
Blog Archive
Stats
Read image from database and write into response
<%@ WebHandler Language="C#" Class="Image" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class Image : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        if (string.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            context.Response.StatusCode = "404";
            context.Response.End();
        }
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[VarHelper.ConnStrLocal].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT [MimeType], [Image] FROM [ImageAlbum] WHERE [ImageID] = @ImageID", conn);
            cmd.Parameters.Add("@ImageID", System.Data.SqlDbType.Int).Value = context.Request.QueryString["id"];

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                byte[] data = null;
                string contentType = string.Empty;
                while (reader.Read())
                {
                    data = (byte[])reader["Image"];
                    contentType = (string)reader["MimeType"];
                }
                context.Response.ContentType = contentType;
                context.Response.BinaryWrite(data);
                context.Response.End();
            }
            else
            {
                context.Response.StatusCode = "404";
                context.Response.End();
            }
            conn.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}