Hello All,
I am storing images in database(sql server 2005) using http handler. But when I run the website on my pc images are displayed in a proper way. I am displayimg image in image field before saving with the help of http handler class. Images are displayed correctly and saved successfully.
But when I have put this website on server, images are not updated and not shown in image box. But should the problem.
You can check online it on www.dritlive.com/admin/updateitem.aspx
Only check in update item only..............
My http handler class is:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
Int32 itemid;
if (context.Request.QueryString["id"] != null)
itemid = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(itemid);
if (strm == null)
{
return;
}
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
public Stream ShowEmpImage(int itemid)
{
string conn = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT item_image FROM TbItem WHERE item_id = @ID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", itemid);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable {
get {
return false;
}
}
}
The code for uploading an image is:
BinaryReader rr;
byte[] a;
protected void Button4_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
rr = new BinaryReader(FileUpload1.PostedFile.InputStream);
a = rr.ReadBytes(FileUpload1.PostedFile.ContentLength);
string path = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("../Admin/Images/" + path));
Image1.ImageUrl = "../Admin/Images/" + path;
Session["a"] = a;
}
else
{
Label6.Text = "please Select Image by Borrowsing";
}
}
catch (Exception ex)
{
}
}