TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sunitha Chandran
NA
38
0
How to display binary images from database in asp.net MVC3?
Oct 9 2012 4:27 AM
@Html.ImageHandler(@dr["id"].ToString())
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.
Reply
Answers (
0
)
Getting text from a Textbox
Internet TRANSACTION Script