Home > ASP.Net > File Manager

File Manager


try this example to manage files in folder in my website

where admin can Download or Delete file

<asp: DataGrid runat="server" id="articleList" Font-Name="Verdana"
      AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
      HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
      HeaderStyle-Font-Size="12pt" HeaderStyle-Font-Bold="True"
      OnDeleteCommand="articleList_DeleteFile" CellPadding="4" Font-Names="Verdana" ForeColor="#333333" AllowPaging="True">
      <asp:ButtonColumn Text="Delete" ButtonType="PushButton"
          <ItemStyle Font-Bold="True" />
        <asp:TemplateColumn HeaderText=" File" >
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
      <asp:BoundColumn DataField="LastWriteTime"
              HeaderText="Upload Date"
              DataFormatString="{0:d}" >
          <ItemStyle HorizontalAlign="Center" />
      <asp:BoundColumn DataField="Length" HeaderText="Size"
          DataFormatString="{0:#,### bytes}" >
          <ItemStyle HorizontalAlign="Right" />
        <asp:TemplateColumn HeaderText="Download">
                <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Bind("Name") %>'
                    ImageUrl="~/images/DownLoads.gif" OnClick="ImageButton1_Click"/>
      <FooterStyle BackColor="#507CD1" Font-Bold="False" ForeColor="White" />
      <EditItemStyle BackColor="#2461BF" />
      <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="False" ForeColor="#333333" />
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
      <AlternatingItemStyle BackColor="White" />
      <ItemStyle BackColor="#EFF3FB" />
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" Font-Size="12pt" ForeColor="White" />
  </asp: DataGrid>
    <br />
<asp:label runat="server" id="lblMessage" Font-Italic="True" ForeColor="Red" Font-Bold="True" /><br />

in code behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Admin_ManageAdsImage : System.Web.UI.Page

    <em>//-- method for download</em>
    protected void DownloadFile(string name)
        string _path = Request.PhysicalApplicationPath + "Ads/" + name;
        System.IO.FileInfo _file = new System.IO.FileInfo(_path);
        if (_file.Exists)
            Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);
            Response.AddHeader("Content-Length", _file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('File not Found');</script>");

    public void Bind()
        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Ads"));

        articleList.DataSource = dirInfo.GetFiles();

    public void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)

    public void articleList_ItemDataBound(object sender, DataGridItemEventArgs e)
        // First, make sure we're NOT dealing with a Header or Footer row
        if (e.Item.ItemType != ListItemType.Header & e.Item.ItemType != ListItemType.Footer)
            //Now, reference the Button control that the Delete ButtonColumn
            //has been rendered to
            Button deleteButton = e.Item.Cells[0].Controls[0] as Button;

            //We can now add the onclick event handler
            deleteButton.Attributes["onclick"] = "javascript:return " + "confirm('Are you sure? " + DataBinder.Eval(e.Item.DataItem, "Name") + "?')";
    public void articleList_DeleteFile(object sender, DataGridCommandEventArgs e)
        //First, get the filename to delete
        string fileName = articleList.DataKeys[e.Item.ItemIndex].ToString();
        lblMessage.Text = "File Deleted" ;
        //You would want to rebind the Directory's files to the DataGrid after
        //deleting the file...
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        string _Name = ((ImageButton)sender).CommandArgument;


Good Luck

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: