Explanation about .ashx page(handler page) with example

Posted by Gokul under Regular Expressions on 11/29/2010 | Points: 10 | Views : 10384 | Status : [Member] | Replies : 5
Hi friends
I am creating a sampl application in web. In this i am uploading the image into DB by using File upload tool. While storing in DB the image that is uploaded is saved in binary format successfully. I want to display the saved image from Db to grid with other dateils as image name and description about the image. I tried using an handler file to display the saved image in grid. But i was not successfull. Pls can any one help me in solving this issue?????????????????????????????????????????????????????

Thanks and Regards,
GokulNath Nithy.



Responses

Posted by: SheoNarayan on: 11/29/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Gokul,

I have written article targetting the solution of your question asked. Hope this will help you solving your problem as I have described how to show the image into the GridView. Read it http://www.dotnetfunda.com/articles/article1084-saving-image-to-database-in-aspnet-and-displaying-to-the-gridview-.aspx .

Thanks and let me know if you are still facing any problem.



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Gokul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gokul on: 11/30/2010 [Member] Starter | Points: 25

Up
0
Down
Hi SHEONARAYAN
Thanks vry much. The code u suggested me works well. But i cant understand the codings in handler page. Can u pls explain in detail which ll b helpful for me to improve my technical knowledge.



Regards
Gokulnath.N


Thanks and Regards,
GokulNath Nithy.

Gokul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SheoNarayan on: 11/30/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Thanks Gokul,

Try creating a generic handler file by right clicking your project and Add new item ... you will notice that the structure will automatically get created you just need to fill your code.

In my code, I have checked for the querystring of autoid if its null then doing nothing else proceeding further and getting the connection string from the web.config file. Retrieving the image data from database using SqlConnection and SqlCommand object and then writing the picture file data as binary as the response.

Try to debug the app I have attached and you will get better understanding.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Gokul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sivaprasadadiraju on: 12/1/2010 [Member] Starter | Points: 25

Up
0
Down
you can use this in code behind to bind image
ProfilePhotoImag.ImageUrl = ~/FetchProfileImage.ashx?Imageid=" + Imageid;

and now create a handler class and write following code

public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.QueryString["Imageid"] != null)
{
Imageid= int.Parse(context.Request.QueryString["Imageid"]);
}
context.Response.ContentType = "image/jpeg";
// connecting to database to get byte array which is stored in database
byte[] barrImg = objMBL.GetProfilePhoto(Imageid);


System.IO.Stream strm = new System.IO.MemoryStream(barrImg);

int byteSeq = strm.Read(barrImg, 0, barrImg.Length);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(barrImg, 0, byteSeq);
byteSeq = strm.Read(barrImg, 0, barrImg.Length);
}
}

SIVA PRASAD ADIRAJU

Gokul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sadnaisik on: 2/6/2013 [Member] Starter | Points: 25

Up
0
Down
Hello,

Can you explain how to use this code behind..
Regards

Gokul, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response