1

I have got 10 images with 10 file uploads on my update.aspx page. What I want to do is the client can change any current image to be replaced by new image using file upload. I can do that for a single file upload by using update button for each , but that will lead to 10 updateimage buttons , I want to acheive that by single update click which should check which fileupload images have been used and replace those with current image in the db.

Any help or suggestion will be appreciated.

2 Answers 2

1

You are going to want to handle the files using the HttpFileCollection class.

On the update button click event:

protected void Update_Click(object sender, EventArgs e)
{
   string filepath = "C:\\Uploads";

   //HttpFileCollection class initialization
   var filesToBeUploaded = Request.Files;

   for (int i = 0; i < filesToBeUploaded.Count; i++)
   {  
      //HttpPostedFile class initialization
      var postedFile = uploadedFiles[i];

      try
      {    
         if (postedFile.ContentLength > 0 )
         {    
            postedFile.SaveAs(filepath + "\\" + 
               System.IO.Path.GetFileName(postedFile.FileName));
         }    
      } 
      catch (Exception Ex)
      {    
         Label1.Text += "Error: <br/>" + Ex.Message;    
      }    
   }    
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can check with this

 if(FileUpload1.HasFile)
{
//your code
}

check all like that

5 Comments

but there are 10 file uploads, therefore i need 10 if, which will be a mess , is there any other wayof doing it
you can set name FileUpload1,FileUpload2,FileUpload3 and use FindControl in loop so u can achieve with single block
u mean imgUpLoad = ((FileUpload)e.Item.FindControl("FileUpload1"));
yup.. and concat loop variable value with ID imgUpLoad = ((FileUpload)tbl1.FindControl("FileUpload"+i));
I think it is not a proper solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.