Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<%@ 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);
        }
        //context.Response.BinaryWrite(buffer);
    }
    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:

C#
BinaryReader rr;
   //Int32 a;
   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));
               //HttpPostedFile image = FileUpload1.PostedFile;
               Image1.ImageUrl = "../Admin/Images/" + path;
               Session["a"] = a;
               // byte[] arr = rr.ReadBytes(FileUpload1.PostedFile.ContentLength);
           }
           else
           {
               Label6.Text = "please Select Image by Borrowsing";
           }
       }
       catch (Exception ex)
       {

        }
    }
Posted
Updated 17-Oct-10 22:30pm
v2

1 solution

Example:

<%@ WebHandler Language="C#" Class="ResizeImageHandler" %>

using System;
using System.Web;
using CFA.Utilities;
using System.Drawing;

public class ResizeImageHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
string path = context.Request.QueryString["FileName"].ToString();
string width = context.Request.QueryString["Width"].ToString();
string height = context.Request.QueryString["height"].ToString();

context.Response.Clear();
context.Response.ContentType = "image/jpeg";

Bitmap bmp = new Bitmap(width, height);
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable {
get {
return false;
}
}

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900