|
|
|
ลบภาพ asp.net(VB) จะทำส่วนของการลบรูปภาพจากในแฟ้มคะเคยทำเป็นแบบ php |
|
|
|
|
|
|
|
รูปมันเก็บเป็น File ไว้หรอครับ ต้องการจะลบ File ภาพ
หรือ จะลบจาก Database อ่ะครับ ^^
|
|
|
|
|
Date :
2009-12-08 10:40:22 |
By :
ksillapapan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลบfileภาพคะ
|
|
|
|
|
Date :
2009-12-08 10:52:50 |
By :
LuckyStar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หน้า default.aspx
Code (C#)
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="แสดง" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="ลบ" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="50px" Height="50px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
CodeBehind ครับ
Code (C#)
using System.IO;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BindGrid()
{
DataTable source = GetAllFile();
GridView1.DataSource = source;
GridView1.DataBind();
}
public DataTable GetAllFile()
{
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("Image/"));
DataTable dt = new DataTable();
dt.Columns.Add("picName", typeof(string));
FileInfo[] files = dir.GetFiles("*.jpg");
foreach (FileInfo file in files)
{
DataRow dr = dt.NewRow();
dr["picName"] = file.Name;
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
Image img = (Image)e.Row.FindControl("Image1");
if (img != null)
{
img.ImageUrl = Server.MapPath("Image/" + DataBinder.Eval(e.Row.DataItem, "picName"));
}
Label lbl = (Label)e.Row.FindControl("Label1");
if (lbl != null)
{
lbl.Text = (string)DataBinder.Eval(e.Row.DataItem, "picName");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGrid();
}
protected void Button2_Click(object sender, EventArgs e)
{
List<FileInfo> files = new List<FileInfo>();
foreach(GridViewRow grv in GridView1.Rows)
{
CheckBox chk = (CheckBox)grv.FindControl("chk");
Label lbl = (Label)grv.FindControl("Label1");
if (chk.Checked)
{
FileInfo file = new FileInfo(Server.MapPath("Image/" + lbl.Text));
files.Add(file);
}
}
foreach (FileInfo fi in files)
{
if (fi.Exists)
{
fi.Delete();
}
}
BindGrid();
}
}
ลองดู นะครับ ^^
ที่มาครับ
https://www.thaicreate.com/asp.net/asp.net-delete-files.html
https://www.thaicreate.com/asp.net/asp.net-list-files-in-directory.html
|
|
|
|
|
Date :
2009-12-08 16:18:37 |
By :
ksillapapan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พี่มีแบบที่มานเปงVBไหมอ่า
|
|
|
|
|
Date :
2009-12-09 11:41:56 |
By :
LuckyStar |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|