Click here to Skip to main content
15,909,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I uploaded a image into a webpage in asp.net using fileupload control.Now when I am refreshing the page or logging out the page the image disappears.Can anyone help me in this issue.

Thanks in advance...
http://www.codeproject.com/script/Answers/Post.aspx?aid=464791#
aspx.cs

C#
protected void Button2_Click(object sender, EventArgs e)
{
    string path = Server.MapPath("croppedImages/");
    if(FileUpload1.HasFile)
    {
        string ext=Path.GetExtension(FileUpload1.FileName);
        if (ext== ".jpg" || ext== ".png")
        {
            FileUpload1.SaveAs(path+FileUpload1.FileName);
            string name = "~/croppedImages/" + FileUpload1.FileName;
            string s = "insert into immage values('"+TextBox1.Text.Trim()+"','"+name+"')";

            SqlCommand cmd= new SqlCommand(s,con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("Your file has been uploaded");
        }
        else
        {
            Response.Write("You can upload only jpg and png files");
        }
    }
    else
    {
        Response.Write("Please select a file");
    }
    Session["ImageBytes"] = FileUpload1.FileBytes;
    ImagePreview.ImageUrl = "~/Handler.ashx";

}
Posted
Updated 24-Sep-12 6:11am
v3

1 solution

The FileUpload control does nothing other than upload the file. When it is performing it's postback you need to save the data to the harddisk for permanent storage.
On page refresh you will need to load the image from the disk again, you can use the asp:Image tag to display it.

See the sample at the bottom of this page for saving a file to the disk using the FileUpload control: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload(v=vs.100).aspx[^]

Hope this helps you on your way.
 
Share this answer
 
Comments
Raghavanand 24-Sep-12 11:55am    
Thanks for your kind reply. My code save the image in filesystem and the path in database.Yet it goes away on page refresh. I will send you the code for your reference
Christiaan Rakowski 24-Sep-12 12:52pm    
At the end of this method you set ImagePreview.ImageUrl = "~/Handler.ashx";
I assume this handler will return the image, if so you will have to do the same in the PageLoad method of the page, just copy that 1 line over to there and it should be good.

Small extra advice: jpeg (with the e) is also a valid jpg image, adding that to your check is probably a good idea.
Raghavanand 25-Sep-12 2:05am    
Thanks a lot Sir.. It worked ..
Raghavanand 25-Sep-12 2:10am    
I have one more problem sir
"System.Drawing.Image ImagePreview = Bitmap.FromFile(@"\croppedimages\", true);"
The above line is showing a file not found exception. I would be really thankful if you help me in this matter
Christiaan Rakowski 25-Sep-12 3:12am    
When you are in ASP the using relative directory doesn't work (most of the time). You have to use string folder = HttpContext.Current.Server.MapPath("/croppedimages/"); to get the folder, then open the image with Bitmap.FromFile(folder + filename, true);

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