• 一个简单的管理Web站点文件的页面程序(修改版)


    蛋疼之余,写了一个小程序,用于在页面中管理当前 Web 站点下的所有文件。可以对所有文件和文件夹执行重命名、删除等功能,也可以创建目录,上传文件,下载文件等。

    不废话,先看效果:

    贴代码:

    FileManager
      1 <!-- 
      2 Author: 张浩华
      3 DateTime: 2012-07-06 03:25
      4 -------------------------------------------------
      5 管理Web站点下文件的页面程序。
      6 提供上传、重命名、删除、创建文件夹、下载等功能。
      7 -------------------------------------------------
      8 于 2012-07-06 16:35 修改。
      9 修改“当前目录”保存方式,取消使用静态变量保存,使用 Session 对象记录当前目录。
     10 添加“当前目录”页面显示,显示当前操作所在目录(根目录名为“Root\”)。
     11 
     12 -->
     13 <%@ Page Language="C#" %>
     14 
     15 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     16 
     17 <script runat="server">
     18     string Msg = string.Empty;
     19 
     20     private string _CURRENT_PATH
     21     {
     22         get { return Session["ManagerCurrentPath"] == null ? Server.MapPath("~/") : Session["ManagerCurrentPath"].ToString(); }
     23         set { Session["ManagerCurrentPath"] = value; }
     24     }
     25     
     26     protected void Page_Load(object sender, EventArgs e)
     27     {
     28         InitFiles();
     29         switch (Request["action"])
     30         {
     31             case "Root":
     32                 Root();
     33                 break;
     34             case "Back":
     35                 Back();
     36                 break;
     37             case "Open":
     38                 Open(Request["FileName"]);
     39                 break;
     40             case "Delete":
     41                 Delete(Request["FileName"]);
     42                 break;
     43         }
     44     }
     45 
     46     protected void btnUpload_Click(object sender, EventArgs e)
     47     {
     48         if (fuFile.HasFile)
     49         {
     50             string currentPath = _CURRENT_PATH;
     51             string fileName = fuFile.FileName;
     52             if (rbCover.Checked)
     53             {
     54             }
     55             else if (rbRename.Checked)
     56             {
     57                 while (System.IO.File.Exists(currentPath + fileName))
     58                 {
     59                     fileName = "new_" + fileName;
     60                 }
     61             }
     62             fuFile.SaveAs(currentPath + fileName);
     63         }
     64         InitFiles();
     65     }
     66 
     67     protected void btnSave_Click(object sender, EventArgs e)
     68     {
     69         string oleFileName = hfOldName.Value;
     70         string newFileName = txtNewName.Text;
     71         if (string.IsNullOrEmpty(newFileName))
     72         {
     73             Msg = "The file name can't for empty !";
     74             return;
     75         }
     76 
     77         string currentPath = _CURRENT_PATH;
     78         string oldPath = currentPath + oleFileName;
     79         string newPath = currentPath + newFileName;
     80         if (IsFile(oldPath))
     81         {
     82             if (System.IO.File.Exists(newPath))
     83             {
     84                 Msg = "The file name repeated, please reset.";
     85                 return;
     86             }
     87             System.IO.File.Move(oldPath, newPath);
     88         }
     89         else
     90         {
     91             if (string.IsNullOrEmpty(oleFileName))
     92             {
     93                 System.IO.Directory.CreateDirectory(newPath);
     94             }
     95             else
     96             {
     97                 System.IO.Directory.Move(oldPath, newPath);
     98             }
     99         }
    100         InitFiles();
    101     }
    102 
    103     private void Back()
    104     {
    105         string path = _CURRENT_PATH;
    106         string parent = new System.IO.DirectoryInfo(path).Parent.FullName + "\\";
    107         if (parent.IndexOf(Server.MapPath("~/")) >= 0)
    108         {
    109             _CURRENT_PATH = parent;
    110         }
    111         Response.Redirect(Request.Url.AbsolutePath);        
    112     }
    113     
    114     private void Delete(string filename)
    115     {
    116         if (string.IsNullOrEmpty(filename)) return;
    117         string currentPath = _CURRENT_PATH;
    118         string path = currentPath + filename;
    119         if (IsFile(path))
    120         {
    121             System.IO.File.Delete(path);
    122         }
    123         else
    124         {
    125             try { System.IO.Directory.Delete(path, false); }
    126             catch { }
    127         }
    128         Response.Redirect(Request.Url.AbsolutePath);
    129     }
    130     
    131     protected string GetCreateTime(string name)
    132     {
    133         string currentPath = _CURRENT_PATH;
    134         string path = currentPath + name;
    135         return System.IO.File.GetCreationTime(path).ToString("yyyy-MM-dd HH:mm:ss");
    136     }
    137     
    138     protected string GetIcon(string name)
    139     {
    140         string currentPath = _CURRENT_PATH;
    141         string path = currentPath + name;
    142         if (IsFile(path))
    143         {
    144             int dotPlace = name.LastIndexOf('.');
    145             if (dotPlace < 0)
    146             {
    147                 return "";
    148             }
    149             else
    150             {
    151                 return name.Substring(dotPlace + 1);
    152             }
    153         }
    154         else
    155         {
    156             return "{DIR}";
    157         }
    158     }
    159 
    160     protected string GetSize(string name)
    161     {
    162         string currentPath = _CURRENT_PATH;
    163         string path = currentPath + name;
    164         if (IsFile(path))
    165         {
    166             long length = new System.IO.FileInfo(path).Length;
    167             return ((length / 1024) + 1) + " KB (" + length + "B)";
    168         }
    169         else
    170         {
    171             return "unknow";
    172         }
    173     }
    174 
    175     protected string GetUpdateTime(string name)
    176     {
    177         string currentPath = _CURRENT_PATH;
    178         string path = currentPath + name;
    179         return System.IO.File.GetLastWriteTime(path).ToString("yyyy-MM-dd HH:mm:ss");
    180     }
    181     
    182     private void InitFiles()
    183     {
    184         string currentPath = _CURRENT_PATH;
    185         string[] directorys = System.IO.Directory.GetDirectories(currentPath);
    186         string[] files = System.IO.Directory.GetFiles(currentPath);
    187         System.Collections.Generic.IList<string> arr = new System.Collections.Generic.List<string>();
    188         foreach (string s in directorys)
    189         {
    190             arr.Add(s.Replace(currentPath, ""));
    191         }
    192         foreach (string s in files)
    193         {
    194             arr.Add(s.Replace(currentPath, ""));
    195         }
    196 
    197         rptFile.DataSource = arr;
    198         rptFile.DataBind();   
    199     }
    200     
    201     private bool IsFile(string path)
    202     {
    203         return System.IO.File.Exists(path);
    204     }
    205 
    206     private void Open(string fileName)
    207     {
    208         string currentpath = _CURRENT_PATH;
    209         string path = currentpath + fileName;
    210 
    211         if (IsFile(path))
    212         {
    213             System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
    214             Response.Clear();
    215             Response.ClearContent();
    216             Response.ClearHeaders();
    217             Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    218             Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    219             Response.AddHeader("Content-Transfer-Encoding", "binary");
    220             Response.ContentType = "application/octet-stream";
    221             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    222             Response.WriteFile(fileInfo.FullName);
    223             Response.Flush();
    224             Response.End();
    225             
    226         }
    227         else
    228         {
    229             _CURRENT_PATH = path + "\\";
    230             Response.Redirect(Request.Url.AbsolutePath); 
    231         }
    232     }
    233     
    234     private void Root()
    235     {
    236         _CURRENT_PATH = Server.MapPath("~/");
    237         Response.Redirect(Request.Url.AbsolutePath); 
    238     }
    239     
    240 </script>
    241 
    242 <html xmlns="http://www.w3.org/1999/xhtml">
    243 <head runat="server">
    244     <title></title>
    245     <style type="text/css">
    246         body{ margin:0; padding:10px; background:#aaf; font-family:Arial 宋体; font-size:14px; }
    247 
    248         #container{ border:1px #ddd solid;}
    249         #menu{ background-color:#ccc; }
    250         #menu .line{ border-left:solid 1px #fff; width:0; }
    251         #menu a{ display:inline-block; margin:0; padding:8px 12px; text-decoration:none;  }
    252         #menu a:active,#menu a:hover{ background-color:#ddd;  }
    253         
    254         #main{ height:350px; }
    255         #main .file{ float:left; width:100px; height:75px; margin:8px; padding:5px; overflow:hidden; text-align:center; }
    256         #main .file .icon{ margin:0 5px; font-family:Impact,Arial Black; font-size:30px; width:85px; height:40px; overflow:hidden; }
    257         #main .file .name{ font-size:12px; }
    258 
    259         #main, #upload_panel, #rename_panel{ background-color:#fff; border:1px #ddd solid; border-left:0; border-right:0; }
    260         #upload_panel, #rename_panel{ padding:10px; }
    261 
    262         #status_bar{ background-color:#ccc; }
    263         #status_bar span{ display:inline-block; margin:0; padding:5px 30px 5px 8px; border-left:solid 1px #ddd; }
    264     </style>
    265     <script type="text/javascript">
    266         var Page = { CurrentFile: null };
    267 
    268         /* 页面加载 */
    269         function PageLoad() {
    270             InitMenu();
    271             InitFile();
    272             InitStatusBar();
    273             InitPanel();
    274             ShowMessage();
    275         }
    276 
    277         /* 初始化菜单 */
    278         function InitMenu() {
    279             /* 页面加载事件,处理初始化页面操作 */
    280             var menuItems = document.getElementById("menu").childNodes;
    281             for (var menu in menuItems) {
    282                 if (menu >= 0 && (menuItems[menu].tagName + "").toUpperCase() == "A") {
    283                     var a = menuItems[menu];
    284                     a.onclick = ClickMenuItem;
    285                 }
    286             }
    287         }
    288 
    289         /* 初始化文件列表事件 */
    290         function InitFile() {
    291             var files = document.getElementById("main").childNodes;
    292             for (var k in files) {
    293                 if (k >= 0 && (files[k].className + "").toLowerCase().indexOf("file") >= 0) {
    294                     var file = files[k];
    295                     file.style.cursor = "pointer";
    296                     file.onclick = ClickFile;
    297                 }
    298             }
    299         }
    300 
    301         /* 初始化“上传文件”和“修改文件名”模块 */
    302         function InitPanel() {
    303             document.getElementById("upload_panel").style.display = "none";  //隐藏上传文件模块
    304             document.getElementById("rename_panel").style.display = "none";  //隐藏修改文件名模块
    305             document.getElementById("fuFile").value = "";  //清空上传文件控件
    306             document.getElementById("txtNewName").value = "";  //清空名称文本框
    307             document.getElementById("hfOldName").value = "";  //清空名称文本框
    308             document.getElementsByName("btnCancel")[0].onclick = InitPanel;  //绑定 Cancel 按钮 Click 事件
    309             document.getElementsByName("btnCancel")[1].onclick = InitPanel;  //绑定 Cancel 按钮 Click 事件
    310         }
    311 
    312         /* 初始化状态栏 */
    313         function InitStatusBar() {
    314             var statusItems = document.getElementById("status_bar").childNodes;
    315             for (var itemKey in statusItems) {
    316                 if (itemKey >= 0 && (statusItems[itemKey].tagName + "").toUpperCase() == "SPAN") {
    317                     var span = statusItems[itemKey];
    318                     var value = Page.CurrentFile == null ? "" : Page.CurrentFile.getAttribute(span.className);
    319                     if ("filename" == span.className.toLowerCase()) span.innerHTML = "FileName: " + value;
    320                     if ("type" == span.className.toLowerCase()) span.innerHTML = "Type: " + value;
    321                     if ("size" == span.className.toLowerCase()) span.innerHTML = "Size: " + value;
    322                     if ("create_time" == span.className.toLowerCase()) span.innerHTML = "CreateTime: " + value;
    323                     if ("update_time" == span.className.toLowerCase()) span.innerHTML = "LastUpdateTime: " + value;
    324                 }
    325             }
    326         }
    327 
    328         /* 单击菜单项事件 */
    329         function ClickMenuItem() {
    330             InitPanel();
    331             var id = this.id;
    332 
    333             switch (id) {
    334                 case "Root":
    335                     location.search = 'action=Root';
    336                     break;
    337                 case "Back":
    338                     location.search = 'action=Back';
    339                     break;
    340                 case "Open":
    341                     Open();
    342                     break;
    343                 case "NewFolder":
    344                     document.getElementById("rename_panel").style.display = "";
    345                     break;
    346                 case "Upload":
    347                     document.getElementById("upload_panel").style.display = "";
    348                     break;
    349                 case "Rename":
    350                     Rename();
    351                     break;
    352                 case "Delete":
    353                     Delete()
    354                     break;
    355             }
    356 
    357             return false;  //不响应超链接跳转操作
    358         }
    359 
    360         /* 单击文件事件 */
    361         function ClickFile() {
    362             if (Page.CurrentFile != null) {
    363                 Page.CurrentFile.style.background = "";
    364             }
    365 
    366             if (Page.CurrentFile == this) {
    367                 Page.CurrentFile.style.background = "";
    368                 Page.CurrentFile = null;
    369             } else {
    370                 this.style.background = "#ddd";
    371                 Page.CurrentFile = this;
    372             }
    373             InitStatusBar();
    374             InitPanel();
    375         }
    376 
    377         function Delete() {
    378             if (Page.CurrentFile != null) {
    379                 location.search = 'action=Delete&FileName=' + Page.CurrentFile.getAttribute("filename");
    380             }
    381         }
    382 
    383         function Open() {
    384             if (Page.CurrentFile != null) {
    385                 location.search = 'action=Open&FileName=' + Page.CurrentFile.getAttribute("filename");
    386             }
    387         }
    388 
    389         function Rename() {
    390             if (Page.CurrentFile != null) {
    391                 document.getElementById("txtNewName").value = Page.CurrentFile.getAttribute("filename");
    392                 document.getElementById("hfOldName").value = Page.CurrentFile.getAttribute("filename");
    393                 document.getElementById("rename_panel").style.display = "";
    394             }
    395         }
    396 
    397         function ShowMessage() {
    398             var msg = "<%=Msg %>";
    399             if (msg != "") {
    400                 alert(msg);
    401             }
    402         }
    403     </script>
    404 </head>
    405 <body onload="PageLoad()">
    406     <form id="form1" runat="server">
    407     <div id="container">
    408         <div id="menu">
    409             <a href="#" id="Root">Root</a>
    410             <a href="#" id="Back">Back</a>
    411             <a href="#" id="Open">Open(Download)</a>
    412             <a class="line"></a>
    413             <a href="#" id="NewFolder">NewFolder</a>
    414             <a href="#" id="Upload">Upload</a>
    415             <a href="#" id="Rename">Rename</a>
    416             <a href="#" id="Delete">Delete</a>
    417             <a class="line"></a>            
    418             <a><%=_CURRENT_PATH.Replace(Server.MapPath("~/"), "Root\\") %></a>
    419         </div>
    
    420         <div id="main">
    421             <asp:Repeater ID="rptFile" runat="server">
    422                 <ItemTemplate>
    423             <div class="file" 
    424                 filename="<%# Container.DataItem %>" 
    425                 size="<%# GetSize(Container.DataItem + "") %>" 
    426                 type="<%# GetIcon(Container.DataItem + "") %>" 
    427                 create_time="<%# GetCreateTime(Container.DataItem + "") %>" 
    428                 update_time="<%# GetCreateTime(Container.DataItem + "") %>">
    429                 <div class="icon"><%# GetIcon(Container.DataItem + "") %></div>
    430                 <div class="name"><%# Container.DataItem %></div>
    431             </div>
    432                 </ItemTemplate>
    433             </asp:Repeater>
    434             <div style="clear:both;"></div>
    435         </div>
    436         <div id="upload_panel">
    437             <asp:FileUpload ID="fuFile" runat="server" />&nbsp;
    438             <asp:RadioButton ID="rbCover" runat="server" GroupName="FileReapter" Text="Cover" Checked="true" />
    439             <asp:RadioButton ID="rbRename" runat="server" GroupName="FileReapter" Text="Rename" />&nbsp;
    440             <asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />&nbsp;
    441             <input name="btnCancel" type="button" value="Cancel" />
    442         </div>
    443         <div id="rename_panel">
    444             <asp:TextBox ID="txtNewName" runat="server" Text="asdf"></asp:TextBox>&nbsp;
    445             <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />&nbsp;
    446             <input name="btnCancel" type="button" value="Cancel" />
    447             <asp:HiddenField ID="hfOldName" runat="server" />
    448         </div>
    449         <div id="status_bar">
    450             <span class="filename"></span>
    451             <span class="type"></span>
    452             <span class="size"></span>
    453             <span class="create_time"></span>
    454             <span class="update_time"></span>
    455         </div>
    456     </div>
    457     </form>
    458 </body>
    459 </html>


     

    不想 Copy 的话,下面附页面下载地址:

    修改版:https://files.cnblogs.com/zhhh/FileManager(%E4%BF%AE%E6%94%B9).zip

  • 相关阅读:
    nginx安装
    win7 mount到Linux下无法分配内存的问题(Cannot allocate memory)
    [转]linux时间同步
    关于文件缓冲的问题
    【转】VNC配置
    yum代理设置
    mysql开启日志
    [译]rabbitmq 2.5 Where’s my message? Durability and you
    [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
    [译]rabbitmq 2.2 Building from the bottom: queues
  • 原文地址:https://www.cnblogs.com/zhhh/p/2578718.html
Copyright © 2020-2023  润新知