我有一个应用程序,其中有一个上传图像页面,我在其中上传图像并将这些图像存储在图像文件夹下的不同文件夹中,并在数据库中存储它的路径。流程是这样的:
- 用户从系统中选择文件
- 用户添加描述
- 用户从下拉列表中选择部门,并根据该选择将图像存储在不同部门的文件夹中。
这是我的 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";
}
}
}
我希望我对你们很清楚
我怎样才能做到这一点?