ASP.Net/Components/File Browser — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:51, 26 мая 2010

Get file list under control folder (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (upImage.HasFile)
        {
            if (CheckFileType(upImage.FileName))
            {
                upImage.SaveAs(MapPath(upImage.FileName));
            }
        }
    }
    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;    
            case ".jpg":
                return true;            
            case ".jpeg":
                return true;
            default:
                return false;        
        }
    }
    void Page_PreRender()
    {
        string upFolder = MapPath(".");
        DirectoryInfo dir = new DirectoryInfo(upFolder);
        dlstImages.DataSource = dir.GetFiles();
        dlstImages.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>FileUpload File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblImageFile"
        Text="Image File:"
        AssociatedControlID="upImage"
        Runat="server" />
    <asp:FileUpload
        id="upImage"
        Runat="server" />
    <br /><br />
    
    <asp:Button
        id="btnAdd"
        Text="Add Image"
        OnClick="btnAdd_Click" 
        Runat="server" />
    <hr />
    
    <asp:DataList
        id="dlstImages"
        RepeatColumns="3"
        runat="server">
        <ItemTemplate>
        <asp:Image ID="Image1" 
            ImageUrl="<%# Eval("Name", "./{0}") %>"
            style="width:200px"
            Runat="server" />
        <br />
        <%# Eval("Name") %>    
        </ItemTemplate>
    </asp:DataList>
    
    </div>
    </form>
</body>
</html>



View file info (C#)

<%@ Page language="c#" src="ViewFiles.aspx.cs" AutoEventWireup="false" Inherits="ViewFiles" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server" Font-Names="Verdana" Font-Bold="True">Files in the FTP Directory:</asp:Label>
      <asp:ListBox id="lstFiles" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server" Width="256px" Height="264px" AutoPostBack="True" Font-Names="Verdana"></asp:ListBox>
      <asp:Button id="cmdRefresh" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 328px" runat="server" Width="72px" Height="32px" Text="Refresh"></asp:Button>
      <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 296px; POSITION: absolute; TOP: 24px" runat="server" Width="248px" Height="24px" Font-Names="Verdana" Font-Bold="True">Selected File Information:</asp:Label>
      <asp:Label id="lblFileInfo" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 56px" runat="server" Width="312px" Height="264px" BorderStyle="Groove" BorderWidth="2px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
      <asp:Button id="cmdDelete" style="Z-INDEX: 106; LEFT: 520px; POSITION: absolute; TOP: 328px" runat="server" Width="89px" Height="32px" Text="Delete File"></asp:Button>
    </form>
  </body>
</HTML>
<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
  public class ViewFiles : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.ListBox lstFiles;
    protected System.Web.UI.WebControls.Button cmdRefresh;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label lblFileInfo;
    protected System.Web.UI.WebControls.Button cmdDelete;
    string ftpDirectory = System.Web.HttpContext.Current.Server.MapPath("yourfolderHere");//@"c:\Temp";
    private void Page_Load(object sender, System.EventArgs e)
    {
      if (!this.IsPostBack)
      {
        CreateFileList();
      }
    }
    private void CreateFileList()
    {
      string[] fileList = Directory.GetFiles(ftpDirectory);
      lstFiles.DataSource = fileList;
      lstFiles.DataBind();
      lblFileInfo.Text = "";
      cmdDelete.Enabled = false;
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }
    
    private void InitializeComponent()
    {    
      this.lstFiles.SelectedIndexChanged += new System.EventHandler(this.lstFiles_SelectedIndexChanged);
      this.cmdRefresh.Click += new System.EventHandler(this.cmdRefresh_Click);
      this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void cmdRefresh_Click(object sender, System.EventArgs e)
    {
          CreateFileList();
    }
    private void cmdDelete_Click(object sender, System.EventArgs e)
    {
    //  File.Delete(lstFiles.SelectedItem.Text);
      //CreateFileList();
    }
    private void lstFiles_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      string fileName = lstFiles.SelectedItem.Text;
      lblFileInfo.Text = "<b>" + fileName + "</b><br>";
      lblFileInfo.Text += "Created : ";
      lblFileInfo.Text += File.GetCreationTime(fileName).ToString();
      lblFileInfo.Text += "<br>Last Accessed : ";
      lblFileInfo.Text += File.GetLastAccessTime(fileName).ToString();
      lblFileInfo.Text += "<br>";
      FileAttributes attributes = File.GetAttributes(fileName);
      if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
      {
        lblFileInfo.Text += "This is a hidden file." + "<br>";
      }
      if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
      {
        lblFileInfo.Text += "This is a read-only file." + "<br>";
      }
      if ((attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
      {
        cmdDelete.Enabled = true;
      }
      else
      {
        cmdDelete.Enabled = false;
      }
    }
  }
--%>



Web based file browser (C#)

<%@ Page language="c#" src="FileBrowser.aspx.cs" AutoEventWireup="false" Inherits="FileBrowser" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:ListBox id="lstDirs" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 72px" runat="server" Width="248px" Height="208px" Font-Names="Verdana"></asp:ListBox>
      <asp:Button id="cmdParent" style="Z-INDEX: 109; LEFT: 176px; POSITION: absolute; TOP: 280px" runat="server" Text="Up One Level" Width="100px" Height="24px"></asp:Button>
      <asp:Button id="cmdShowInfo" style="Z-INDEX: 107; LEFT: 312px; POSITION: absolute; TOP: 280px" runat="server" Text="Show Info"></asp:Button>
      <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 312px; POSITION: absolute; TOP: 56px" runat="server" Width="192px" Height="24px" Font-Names="Verdana" Font-Bold="True" Font-Size="X-Small">Contained Files:</asp:Label>
      <asp:ListBox id="lstFiles" style="Z-INDEX: 102; LEFT: 312px; POSITION: absolute; TOP: 72px" runat="server" Width="280px" Height="200px" Font-Names="Verdana"></asp:ListBox>
      <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 56px" runat="server" Width="216px" Height="38px" Font-Names="Verdana" Font-Bold="True" Font-Size="X-Small">Subdirectories:</asp:Label>
      <asp:Label id="Label3" style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 16px" runat="server" Width="184px" Height="19px" Font-Names="Verdana" Font-Bold="True" Font-Size="Medium">Current Directory:</asp:Label>
      <asp:Button id="cmdBrowse" style="Z-INDEX: 106; LEFT: 32px; POSITION: absolute; TOP: 280px" runat="server" Text="Browse To Selected" Width="136px" Height="24px"></asp:Button>
      <asp:Label id="lblFileInfo" style="Z-INDEX: 108; LEFT: 312px; POSITION: absolute; TOP: 328px" runat="server" Width="280px" Height="80px" BorderStyle="Groove" BorderWidth="2px" Font-Names="Verdana" Font-Size="X-Small"></asp:Label>
      <asp:Label id="lblCurrentDir" style="Z-INDEX: 110; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server" Width="184px" Height="24px" Font-Names="Verdana" Font-Bold="True" Font-Size="Medium"></asp:Label>
    </form>
  </body>
</HTML>

<%-- FileBrowser.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
  public class FileBrowser : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.ListBox lstDirs;
    protected System.Web.UI.WebControls.Button cmdParent;
    protected System.Web.UI.WebControls.Button cmdShowInfo;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.ListBox lstFiles;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Button cmdBrowse;
    protected System.Web.UI.WebControls.Label lblFileInfo;
    protected System.Web.UI.WebControls.Label lblCurrentDir;
  
    private void Page_Load(object sender, System.EventArgs e)
    {
      if (!this.IsPostBack)
      {
        string startingDir = System.Web.HttpContext.Current.Server.MapPath("yourfolderHere");//@"c:\Temp";
        lblCurrentDir.Text = startingDir;
        ShowFilesIn(startingDir);
        ShowDirectoriesIn(startingDir);
      }
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdParent.Click += new System.EventHandler(this.cmdParent_Click);
      this.cmdShowInfo.Click += new System.EventHandler(this.cmdShowInfo_Click);
      this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void ShowFilesIn(string dir)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(dir);
      lstFiles.Items.Clear();
      foreach (FileInfo fileItem in dirInfo.GetFiles())
      {
        lstFiles.Items.Add(fileItem.Name);
      }
    }
    private void ShowDirectoriesIn(string dir)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(dir);
      lstDirs.Items.Clear();
      foreach (DirectoryInfo dirItem in dirInfo.GetDirectories())
      {
        lstDirs.Items.Add(dirItem.Name);
      }
    }
    private void cmdShowInfo_Click(object sender, System.EventArgs e)
    {
      if (lstFiles.SelectedIndex != -1)
      {
        string fileName = Path.rubine(lblCurrentDir.Text, lstFiles.SelectedItem.Text);
        FileInfo selFile = new FileInfo(fileName);
        lblFileInfo.Text = "<b>" + selFile.Name + "</b><br>";
        lblFileInfo.Text += "Size: " + selFile.Length + "<br>";
        lblFileInfo.Text += "Created: ";
        lblFileInfo.Text += selFile.CreationTime.ToString();
        lblFileInfo.Text += "<br>Last Accessed: ";
        lblFileInfo.Text += selFile.LastAccessTime.ToString();
      }
    }
    private void cmdParent_Click(object sender, System.EventArgs e)
    {
      if (Directory.GetParent(lblCurrentDir.Text) != null){
        string newDir = Directory.GetParent(lblCurrentDir.Text).FullName;
        lblCurrentDir.Text = newDir;
        ShowFilesIn(newDir);
        ShowDirectoriesIn(newDir);
      }
    }
    private void cmdBrowse_Click(object sender, System.EventArgs e)
    {
      if (lstDirs.SelectedIndex != -1)
      {
        string newDir = Path.rubine(lblCurrentDir.Text, lstDirs.SelectedItem.Text);
        lblCurrentDir.Text = newDir;
        ShowFilesIn(newDir);
        ShowDirectoriesIn(newDir);
      }
    }
  }

--%>