Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp.net project.i am using a file uploader, i want to preview the image in a panel or div what i selected in the fileuploader control, what is the solution?
Posted

1 solution

add a httphandler class

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

using System;
using System.Web;

public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

   public void ProcessRequest (HttpContext context) {
    //Checking whether the imagebytes session variable have anything else not doing anything

       if ((context.Session["ImageBytes"]) != null)
       {
           byte[] image = (byte[])(context.Session["ImageBytes"]);
           context.Response.ContentType = "image/JPEG";
           context.Response.BinaryWrite(image);
       }
   }

   public bool IsReusable {
       get {
           return false;
       }
   }

}



and in the preview button click
C#
protected void btnPreview_Click(object sender, EventArgs e)

   {

       Session["ImageBytes"] = PhotoUpload.FileBytes;

       ImagePreview.ImageUrl = "~/ImageHandler.ashx";

   }



Here PhotoUpload is my File Upload control and ImagePreview is asp:image control.
 
Share this answer
 

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