Show multiple file of your folder,download and delete from server in C#
ASPX PAGE :-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files found">
<RowStyle BackColor="Wheat" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="Files" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White" />
<asp:TemplateField HeaderText="Save" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="save" Width="70px" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White">
<ItemTemplate>
<itemtemplate>
<asp:LinkButton ID="lnkDelete" Text="Remove" width="100px" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DeleteFile"></asp:LinkButton>
</itemtemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
NAMESPACES USED :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
CODE BEHIND ASPX.CS :-
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
ASPX PAGE :-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files found">
<RowStyle BackColor="Wheat" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="Files" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White" />
<asp:TemplateField HeaderText="Save" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="save" Width="70px" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove" HeaderStyle-BackColor="DarkOrange" HeaderStyle-ForeColor="White">
<ItemTemplate>
<itemtemplate>
<asp:LinkButton ID="lnkDelete" Text="Remove" width="100px" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DeleteFile"></asp:LinkButton>
</itemtemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
NAMESPACES USED :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
CODE BEHIND ASPX.CS :-
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
No comments:
Post a Comment