Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am storing user images to folder on registration and save its path to database. Later on i have one update page in which user can update its previous image by browse new image. I am able to store user new image to folder and update its path to database but the previous user image is still present in the folder. How to delete it when user upload a new image in folder.
ASP.NET
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btn_upload1" runat="server" Text="Update" onclick="btn_upload1_Click" />

C#
protected void btn_upload1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.HasFile)
            {
                fileName1 = FileUpload1.FileName;
                filePath1 = Server.MapPath("Image/" + System.Guid.NewGuid() + fileName1);
                FileUpload1.SaveAs(filePath1);
                int getPos = filePath1.LastIndexOf("\\");
                int len = filePath1.Length;
                getPath1 = filePath1.Substring(getPos, len - getPos);
                pathToStore1 = getPath1.Remove(0, 1);
                con.Open();
                SqlCommand cmd = new SqlCommand("sps_uploadphoto", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@flag", 1);
                cmd.Parameters.AddWithValue("@ad_id", ad_id);
                cmd.Parameters.AddWithValue("@useremail", ses);
                cmd.Parameters.AddWithValue("@imagedata", pathToStore1);
                cmd.Parameters.AddWithValue("@updatetime", DateTime.Now);
                cmd.ExecuteNonQuery();
                con.Close();                
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('Please browse file.');", true);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
Posted
Comments
[no name] 23-May-14 16:37pm    
Can't you use File.Delete for some reason to delete the file?
Raj Negi 23-May-14 17:19pm    
i dont know how to check current image path value to the folder images name and delete the matched image before update new path value to the database.
[no name] 23-May-14 19:59pm    
What? Do you realize that makes no sense at all? You said yourself that you are saving the path to the database. So what could possibly be the problem?
Raj Negi 24-May-14 3:28am    
i got the solution, i just have to check the image path with the folder images name before perform update. because if i update first then my image path is updated with new one and i am not able to check it. SO first check and if found delete it and then update to database. Thats the answer.
j snooze 23-May-14 17:52pm    
This doesn't make sense, if you are making a new unique GUID based file name for each image, how do you know which image the user is replacing? How do you know they are replacing an image and not just adding a new one per your response below where a person can have multiple images?

Best way is to name the image with the userEmail+filename1 instead of using Guid.
Next time the user comes back to upload a new image, get all the image names in a string array by iterating the image folder and search for any image name that contains the userEmail.

If you find it, delete it and then save the new Image in the folder.

Hope that this will clear your roadblock.
 
Share this answer
 
Comments
Raj Negi 23-May-14 17:17pm    
actually i have 5 images for 1 ID so this not gonna work. I need to check database current image path column value into my folder and delete the image before going to update with new path. But Dont know how to do it...
use the function delete of Class..File as follows:
Assuming that edited.jpg exist in the folder Imgpost.


C#
string abc = Server.MapPath("~/ImgPost/edited.jpg");
File.Delete(abc);
 
Share this answer
 
Comments
Raj Negi 24-May-14 3:34am    
That is i know how to delete image from folder, but in my question i want to delete previous image when i upload new image for same ID. Anyway i got the answer. Thanks
Pranav-BiTwiser 24-May-14 3:43am    
u welcome
i got the solution, i just have to check the image path with the folder images name before perform update. because if i update first then my image path is updated with new one and i am not able to check it. SO first check and if found delete it and then update to database.
C#
protected void btn_upload1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.HasFile)
            {
                SqlCommand cmd2 = new SqlCommand("sps_addetails", con);
                cmd2.CommandType = CommandType.StoredProcedure;
                cmd2.Parameters.AddWithValue("@ad_id", ad_id);
                cmd2.Parameters.AddWithValue("@useremail", ses);
                con.Open();
                SqlDataReader dr = cmd2.ExecuteReader();
                while (dr.Read())
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(dr["imagepath1"])))
                    {
                        string imgpath = Convert.ToString(dr["imagepath1"]);
                        string abc = Server.MapPath("~/Image/" + imgpath);
                        File.Delete(abc);
                    }                    
                }
                con.Close();

                fileName1 = FileUpload1.FileName;
                filePath1 = Server.MapPath("Image/" + System.Guid.NewGuid() + fileName1);
                FileUpload1.SaveAs(filePath1);
                int getPos = filePath1.LastIndexOf("\\");
                int len = filePath1.Length;
                getPath1 = filePath1.Substring(getPos, len - getPos);
                pathToStore1 = getPath1.Remove(0, 1);
                con.Open();
                SqlCommand cmd = new SqlCommand("sps_uploadphoto", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@flag", 1);
                cmd.Parameters.AddWithValue("@ad_id", ad_id);
                cmd.Parameters.AddWithValue("@useremail", ses);
                cmd.Parameters.AddWithValue("@imagedata", pathToStore1);
                cmd.Parameters.AddWithValue("@updatetime", DateTime.Now);
                cmd.ExecuteNonQuery();
                con.Close();                
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('Please browse file.');", true);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
 
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