Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am uploading an image and store in images folder but picture iz not display when i press button.image save but not showing in image control...
please help mee

C#
protected void Button2_Click(object sender, EventArgs e)
       {

           StartUpLoad();
       }

           private void StartUpLoad() {
                //get the file name of the posted image
                string imgName = FileUpload1.FileName;
                //sets the image path
                string imgPath = "Images/" + imgName;
               //get the size in bytes that

               int imgSize = FileUpload1.PostedFile.ContentLength;

               //validates the posted file before saving
               if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
               {
                   // 10240 KB means 10MB, You can change the value based on your requirement
                   if (FileUpload1.PostedFile.ContentLength > 10240) {
                       Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

                }
                   else
                   {
                       //then save it to the Folder
                       FileUpload1.SaveAs(Server.MapPath(imgPath));
                       Image1.ImageUrl = "~/images/" + imgPath;
                       Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

                   }
                   
               }


           }
Posted
Updated 23-May-13 18:57pm
v2
Comments
Prasad Khandekar 24-May-13 1:43am    
Hello,

You are saving image at path Images/, but the path you are assigning to the image control is images/Images. change it to "~/images/ + imgName.

Regards,

Use
Image1.ImageUrl = "~/images/" + imgName;

at the place of
Image1.ImageUrl = "~/images/" + imgPath;

if you use above code ur imageurl becomes ~/images/images/imagename ..hope ur are getting point.ur adding same folder name twise.
 
Share this answer
 
Comments
fak_farrukh 24-May-13 2:45am    
but when i run project image control show nothing i want is shows the last image that was saved
Pallavi Waikar 24-May-13 3:35am    
best way save lastuploaded imge as lastimage in folder .At every upload delete privous image and save new image with same name
private void StartUpLoad() {
//get the file name of the posted image
string imgName = FileUpload1.FileName;
//sets the image path
string imgPath = "Images/" + imgName;
//get the size in bytes that

int imgSize = FileUpload1.PostedFile.ContentLength;

//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
// 10240 KB means 10MB, You can change the value based on your requirement
if (FileUpload1.PostedFile.ContentLength > 10240) {
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

}
else
{
//then save it to the Folder
if(File.Exists(Server.MapPath("~/images/lastimage.jpg"){
File.Delete(Server.MapPath("Images/lastimage.jpg");
}
FileUpload1.SaveAs(Server.MapPath("Images/lastimage.jpg"));
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = "~/images/" + imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

}

}


}

and in html design add
<img id="img1" runat="server" src='~/Images/lastimage.jpg' alt='' style="height: 100px;
width: 100px;" />
OR u have save it in databse and on page load retrive it by number id top 1 order by id desc
fak_farrukh 24-May-13 3:51am    
thank yu :)
Hi...
try this, may help ful to u.

Image1.ImageUrl = "imgPath";
Image1.Databind();

thank u.
 
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