0

我有一个应用程序,其中有一个上传图像页面,我在其中上传图像并将这些图像存储在图像文件夹下的不同文件夹中,并在数据库中存储它的路径。流程是这样的:

  1. 用户从系统中选择文件
  2. 用户添加描述
  3. 用户从下拉列表中选择部门,并根据该选择将图像存储在不同部门的文件夹中。

这是我的 uploadImage.cs 页面代码:

在此页面中,首先我们检查我们在 Image/Department 文件夹下是否有文件夹,如果没有,则我们在该部门下创建文件夹并存储图像,否则如果已经在该部门下创建了该存储图像

protected void btnSubmit_Click1(object sender, EventArgs e)
{
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebGallery"].ConnectionString;
    string Description = tbImageName.Text.Trim();
    string Priority = lblPriority.Text.Trim();
    //Get Filename from fileupload control
    string imgName = fileuploadimages.FileName.ToString();
    //sets the image path
    string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
    bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
    if (!IsExists)
        System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
    //then save it to the Folder
    fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
    //Open the database connection
    con.Open();
    //Query to insert images name and Description into database
    SqlCommand cmd = new SqlCommand("insert into Images(ImageName, Description, Path, Priority) values (@ImageName, @Description, @Path, @Priority)", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@ImageName", imgName);
    cmd.Parameters.AddWithValue("@Description", Description);
    cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
    cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    tbImageName.Text = string.Empty;
}

在此页面中,我们创建、编辑、更新和删除部门。现在,当用户单击删除按钮时,我想删除该文件夹,以便该文件夹下的所有图像也将删除。

我的 departmentMaste.cs 页面代码:

protected void BindEmployeeDetails()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * from Department_Master", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        gvDetails.DataSource = ds;
        gvDetails.DataBind();
    }
    else
    {
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        gvDetails.DataSource = ds;
        gvDetails.DataBind();
        int columncount = gvDetails.Rows[0].Cells.Count;
        gvDetails.Rows[0].Cells.Clear();
        gvDetails.Rows[0].Cells.Add(new TableCell());
        gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
        gvDetails.Rows[0].Cells[0].Text = "No Records Found";
    }

}

protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvDetails.EditIndex = e.NewEditIndex;
    BindEmployeeDetails();
}

protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //int id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
    string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();
    TextBox txtDepartment = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDepartment");
    con.Open();
    SqlCommand cmd = new SqlCommand("update Department_Master set DepartmentName='" + txtDepartment.Text + "'where ID=" + id, con);
    cmd.ExecuteNonQuery();
    con.Close();
    lblresult.ForeColor = Color.Green;
    lblresult.Text = id + " Details Updated successfully";
    gvDetails.EditIndex = -1;
    BindEmployeeDetails();
}

protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvDetails.EditIndex = -1;
    BindEmployeeDetails();
}

protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    //int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
    string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("delete from Department_Master where ID=" + id, con);
    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
        BindEmployeeDetails();
        lblresult.ForeColor = Color.Red;
        lblresult.Text = id + " details deleted successfully";
    }
}

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //getting username from particular row
        string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"));
        //identifying the control in gridview
        ImageButton lnkbtnresult = (ImageButton)e.Row.FindControl("imgbtnDelete");
        //raising javascript confirmationbox whenver user clicks on link button
        if (lnkbtnresult != null)
        {
            lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + id + "')");
        }

    }
}

protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("AddNew"))
    {

        TextBox txtDepartment = (TextBox)gvDetails.FooterRow.FindControl("txtDepartment");

        con.Open();
        SqlCommand cmd =
            new SqlCommand("insert into Department_Master values('" + txtDepartment.Text + "')", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();
            lblresult.ForeColor = Color.Green;
            lblresult.Text = txtDepartment.Text + " Details inserted successfully";
        }
        else
        {
            lblresult.ForeColor = Color.Red;
            lblresult.Text = txtDepartment.Text + " Details not inserted";
        }
    }
}

我希望我对你们很清楚

我怎样才能做到这一点?

4

2 回答 2

1

更新您的 RowDeleting 事件。

protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
            string id = gvDetails.DataKeys[e.RowIndex].Values["ID"].ToString();



            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from Department_Master where id=" + id, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            DataTable dt = ds.Table[0];
            for (int i = 0; i < dt.rows.count; i++)
            {
                string imgPath = "Images/Departments/" + "" + dt.rows[i]["DepartmentName"] + "/";
                bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
                if (IsExists)
                    System.IO.Directory.Delete(Server.MapPath(imgPath),true);
            }

            con.Close();


            con.Open();
            SqlCommand cmd = new SqlCommand("delete from Department_Master where ID=" + id, con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                lblresult.ForeColor = Color.Red;
                lblresult.Text = id + " details deleted successfully";
            }
            BindEmployeeDetails();
        }

对于弹出消息,

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //getting username from particular row
        string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"));
        //identifying the control in gridview
        ImageButton lnkbtnresult = (ImageButton)e.Row.FindControl("imgbtnDelete");
        //raising javascript confirmationbox whenver user clicks on link button
        if (lnkbtnresult != null)
        {
            lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + id + "');");
        }

    }

}

只要确保“ConfirmationBox”方法是正确的,以提高确认窗口。

要从Images表格中删除图像,您应该参考Department_Master表格。就像一个名为 DepartmentID 的列,其中外键引用到Images表中。即使你将记录插入Images表中,也插入相应DepartmentID的,一旦你完成了所有这些事情,你可以在Images表上运行删除命令

 SqlCommand cmd = new SqlCommand("Delete from Images where DepartmentID="+id +"; delete from Department_Master where ID=" + id, con);

将此代码添加到 aspx 页面中

<script type="text/javascript">
function ConfirmationBox(id)
{
    return confirm('Are you sure to delete department Id:'+ id +'?' );
}
</script>
于 2013-07-01T14:45:55.213 回答
0

使用GetFiles从目录中获取文件,并使用GetParent从图像中获取目录。

string dir = Directory.GetParent(photoPathFromDB).FullName;
string[] files = Directory.GetFiles(dir);
 foreach(string file in files)
     //do stuff
于 2013-07-01T14:07:22.850 回答