using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CabAutomationSystem.Infrastructure
{
public static class HtmlHelpers
{
public static MvcHtmlString ImageHandler(this HtmlHelper helpers, string id)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", "../ImageConverter.ashx?id=" + id);
builder.MergeAttribute("alt", "Picture");
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace CabAutomationSystem.Handlers
{
/// <summary>
/// Summary description for ImageConverter
/// </summary>
public class ImageConverter : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string id = context.Request.QueryString["id"]; // queryString that will be sent to handler
// this is the parameter that we will use
// to retrieve the student image from the database
if (id != null)
{
using (var memoryStream = new MemoryStream())
{
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SLMFConnection"].ConnectionString);
var com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = "select photo from rip_staff where id=@id"; // sql query
com.Parameters.AddWithValue("@id", id);
conn.Open();
SqlDataReader reader = com.ExecuteReader();
reader.Read();
byte[] file = (byte[])reader[0];
memoryStream.Write(file, 0, file.Length);
//context.Response.Buffer = true;
context.Response.BinaryWrite((byte[])reader[0]);
context.Response.End();
reader.Close();
conn.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
this is not working .please help me .thanks in advance.