It is posible to retrieve image from database like in asp.net handler or better approach?

0
0

It is posible to retrieve image from database like in asp.net handler or better approach?
I want to display images stored in database/binary form from html/js, like previously with an asp.net handler
==============================
Something like in client html/js :

img class=”avatar” src=”~/ShowImage.ashx?id=” + data.id + ‘.png”

==============================
In server handler demo code:

public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 empno;
if (context.Request.QueryString[“id”] != null)
empno = Convert.ToInt32(context.Request.QueryString[“id”]);
else
throw new ArgumentException(“No parameter specified”);

context.Response.ContentType = “image/png”; //”image/jpeg”;
Stream strm = ShowEmpImage(empno);
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 empno)
{
string conn = ConfigurationManager.ConnectionStrings[“EmployeeConnString”].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = “SELECT empimg FROM EmpDetails WHERE empid = @ID”;
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue(“@ID”, empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
}

  • You must to post comments
1
0

Of course 🙂

You can

1- Use a PictureBox and assign  the Image property to an Image created from the database.

2- Use a PictureBox and assign the ImageSource to an image file name saved from  the database.

3- Use a class derived from PictureBox, add “: Wisej.Core.IWisejHandler” and assign the ImageSource to this.GetPostbackUrl(), then implement IWisejHandler.ProcessRequest() as in any handler, the parameter is HttpContext.

4- Create a component on a page (or form), or add “: IWisejHandler” to the page (or form) and then you can pass this.GetPostbackUrl() to any <img src> tag you can place in any control (Label, Button, DataGridViewCell) than has AllowHtml. You can also add parameters to this.GetPostbackUrl() + “&img-name=test”, for example, and have your IWisejHandler.ProcessRequest() return an image depending on the parameters.

And infinite combinations of the above.

 

  • Ser Gar
    Awesome. Maybe number 4 fit my requirement, i’m going to try it, because i have OrgChart widget (append js dinamic img tags – for each dynamic node), and I want to pass to [js img tag src dynamic] html content, a generic handler. Plus I guess/hope this way browser uses image caching. Better approach in this case? Thanks in advance!
  • Ser Gar
    Maybe another approach: Is it posible to have a generic handler? (visible from any page or form) (not wisej page specific, to avoid to use “this.GetPostbackUrl()”). And then call from everywhere like “img src=generichandler(parameter_1,parameter_n …)” and that returns a generic custom image
  • Luca (ITG)
    You need an instance of a component to handle the request. For a generic handler you can always add an ashx handler. If you have a Wisej.Web.Widget importing a OrgChart library it’s a lot better to create a derived class and handle requests specific to that widget in that class. Browser caching is based on the URL and cache settings, it doesn’t matter what you return, unless you add cache headers.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.