Click here to Skip to main content
15,896,448 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm uploading an image from diectory.it upload the picture but when i logout it show empty no picture is visible.i'm using session to store the picture . i also want when it store the picture it store with the name of session
please help me with that..

C#
protected void Page_Load(object sender, EventArgs e)
        {
            //Session.Timeout = 90;

            string username = (string)(Session["UserAuthentication"]);
                if(Session["UserAuthentication"]!=null)
                {
                    Label2.Text=username;

                   // data();

                  Image1.ImageUrl = "ImageHandler.ashx?ID=" + Session["UserAuthentication"].ToString();


                }
            else
                {
                    Response.Redirect("Login.aspx");
                }


C#
public class ImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            ////if ((context.Session["UserAuthentication"]) != null)
            ////{
            ////    byte[] image = (byte[])(context.Session["UserAuthentication"]);
            ////    context.Response.ContentType = "image/JPEG";
            ////    context.Response.BinaryWrite(image);
            ////}
            //write your handler implementation here.

            string imagePath = context.Request.QueryString["ID"];

            // split the string on periods and read the last element, this is to ensure we have
            // the right ContentType if the file is named something like "image1.jpg.png"
            System.IO.FileInfo imageFile = new System.IO.FileInfo(context.Request.PhysicalApplicationPath + imagePath);
            bool fileExists = imageFile.Exists;
            if (fileExists)
            {
                context.Response.ContentType = "Images/" + imageFile.Extension;
                context.Response.WriteFile(context.Server.MapPath(imagePath));
            }
            else
            {
                context.Response.ContentType = "Images/";
                context.Response.WriteFile(context.Server.MapPath(imagePath));
            }

            context.Response.Write(imagePath);

        }


C#
protected void Button2_Click1(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
                    Session["UserAuthentication"] = Session["UserAuthentication"] + ext;
                    FileUpload1.SaveAs(Server.MapPath("~/") + Session["UserAuthentication"]);
                    Label2.Text = "Upload status: File uploaded!";
                    Image1.ImageUrl = "ImageHandler.ashx?ID=" + Session["UserAuthentication"].ToString();
                }
                catch (Exception ex)
                {
                    Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
Posted
Comments
So, what is the issue? Are you facing any problems?
fak_farrukh 10-Jun-13 3:05am    
problem is that it is not showing picture after logout.means when i again login from that id it shows nothing
Can you put a debugger on the below line...and login for that user. on page load it will hit debugger.
bool fileExists = imageFile.Exists;

and check what is the value of imageFile and imagePath here and match that with the physical location of the images. Are they matching?
fak_farrukh 10-Jun-13 4:51am    
the file saves like this for example: john.jpeg
but session value is john so it does not identify
So, that's the problem. Now think of some logic to append the file extension to that, so that you can find the image.

The logic may be, first search any image with the name and then find its extension and load the image.

Since I cannot debug your code and here I am unable to find the cause of not showing the image, I am giving the links of similar articles. Please refer the links below:
Save and Retrieve Images from the Database using ASP.NET [^]
How to retrieve saved images from SQL Server database and show on the Page[^]


--Amit
 
Share this answer
 
The problem is you are saving the file with the Session value appended with the extension of file.
C#
Session["UserAuthentication"] = Session["UserAuthentication"] + ext;
FileUpload1.SaveAs(Server.MapPath("~/") + Session["UserAuthentication"]);


But you are searching it with the Session value only.
C#
string imagePath = context.Request.QueryString["ID"];


So, its not getting the image.

Try to append the file extension to the Session value, then you can get the image.
For that you may need to search the image with the Session value and then get it.
 
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