Home > ASP.Net > File Manager

File Manager


Hi

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"
      DataKeyField="FullName"
      OnItemDataBound="articleList_ItemDataBound"
      OnDeleteCommand="articleList_DeleteFile" CellPadding="4" Font-Names="Verdana" ForeColor="#333333" AllowPaging="True">
    <Columns>
      <asp:ButtonColumn Text="Delete" ButtonType="PushButton"
              CommandName="Delete">
          <ItemStyle Font-Bold="True" />
      </asp:ButtonColumn>
        <asp:TemplateColumn HeaderText=" File" >
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
      <asp:BoundColumn DataField="LastWriteTime"
              HeaderText="Upload Date"
              DataFormatString="{0:d}" >
          <ItemStyle HorizontalAlign="Center" />
      </asp:BoundColumn>
      <asp:BoundColumn DataField="Length" HeaderText="Size"
          DataFormatString="{0:#,### bytes}" >
          <ItemStyle HorizontalAlign="Right" />
      </asp:BoundColumn>
        <asp:TemplateColumn HeaderText="Download">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Bind("Name") %>'
                    ImageUrl="~/images/DownLoads.gif" OnClick="ImageButton1_Click"/>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
      <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.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);
            Response.AddHeader("Content-Length", _file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(_file.FullName);
            Response.End();
        }
        else
        {
            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();
        articleList.DataBind();
    }

    public void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();  
        }
    }

    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();
        File.Delete(fileName);
        lblMessage.Text = "File Deleted" ;
        Bind();
        //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;
        DownloadFile(_Name);

    }
}

Good Luck

Advertisements
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: