从文件夹中删除图像 [英] Delete image from folder

查看:113
本文介绍了从文件夹中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在注册时将用户图像存储到文件夹,并将其路径保存到数据库。稍后,我有一个更新页面,用户可以通过浏览新图像更新其以前的图像。我能够将用户新图像存储到文件夹并更新其到数据库的路径,但以前的用户图像仍然存在于该文件夹中。当用户在文件夹中上传新图像时如何删除它。

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:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btn_upload1" runat="server" Text="Update" onclick="btn_upload1_Click" />




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;
        }
    }

推荐答案

最好的方法是使用userEmail + filename1命名图像,而不是使用Guid 。

下次用户返回上传新图像时,通过迭代图像文件夹并搜索包含userEmail的任何图像名称,获取字符串数组中的所有图像名称。



如果你找到它,删除它然后将新图像保存在文件夹中。



希望这会清除你的包版。
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.


使用Class..File函数删除如下:

假设edited.jpg存在于Imgpost文件夹中。





use the function delete of Class..File as follows:
Assuming that edited.jpg exist in the folder Imgpost.


string abc = Server.MapPath("~/ImgPost/edited.jpg");
File.Delete(abc);


我得到了解决方案,我只需要在执行更新之前检查文件夹图像名称的图像路径。因为如果我先更新然后我的图像路径更新了新的,我无法检查它。首先检查,如果发现删除它,然后更新到数据库。

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.
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;
        }
    }


这篇关于从文件夹中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆