A simple asp.net program for managing Web site files

Source: Internet
Author: User
Tags tagname

First look at the effect

Webfilemanager

The code is as follows Copy Code

<!--
Author: Zhang Haohua
datetime:2012-07-06 03:25
-----------------------------------
A page program that manages files under a Web site.
Provide features such as uploading, renaming, deleting, creating folders, downloading, and so on.
-->
<%@ Page language= "C #"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

&lt;script runat= "Server" &gt;


String Msg = String. Empty;


static string _current_path = "";





protected void Page_Load (object sender, EventArgs e)


{


Initfiles ();


Switch (request["action"])


{


Case "Root":


Root ();


Break


Case "Back":


Back ();


Break


Case "Open":


Open (request["FileName"]);


Break


Case "Delete":


Delete (request["FileName"]);


Break


}


}

protected void Btnupload_click (object sender, EventArgs e)


{


if (fufile.hasfile)


{


String currentpath = Getcurrentpath ();


string fileName = Fufile.filename;


if (rbcover.checked)


{


}


else if (rbrename.checked)


{


while (System.IO.File.Exists (Currentpath + fileName))


{


filename = "New_" + filename;


}


}


Fufile.saveas (Currentpath + fileName);


}


Initfiles ();


}

protected void Btnsave_click (object sender, EventArgs e)


{


string olefilename = Hfoldname.value;


string newfilename = Txtnewname.text;


if (string. IsNullOrEmpty (NewFileName))


{


MSG = "The file name can ' t for empty!";


Return


}





String currentpath = Getcurrentpath ();


String OldPath = Currentpath + olefilename;


String NewPath = Currentpath + newfilename;


if (Isfile (OldPath))


{


if (System.IO.File.Exists (NewPath))


{


MSG = "The file name repeated, please reset."


Return


}


System.IO.File.Move (OldPath, NewPath);


}


Else


{


if (string. IsNullOrEmpty (Olefilename))


{


System.IO.Directory.CreateDirectory (NewPath);


}


Else


{


System.IO.Directory.Move (OldPath, NewPath);


}


}


Initfiles ();


}

private void Back ()


{


String path = Getcurrentpath ();


String parent = new System.IO.DirectoryInfo (path). Parent.fullname + "\";


if (parent. IndexOf (Server.MapPath ("~/")) &gt;= 0)


{


_current_path = parent;


}


Response.Redirect (Request.Url.AbsolutePath);


}





private void Delete (string filename)


{


if (string. IsNullOrEmpty (filename)) return;


String currentpath = Getcurrentpath ();


String path = Currentpath + filename;


if (isfile (path))


{


System.IO.File.Delete (path);


}


Else


{


try {System.IO.Directory.Delete (path, false);}


Catch {}


}


Response.Redirect (Request.Url.AbsolutePath);


}





Protected string Getcreatetime (string name)


{


String currentpath = Getcurrentpath ();


String path = Currentpath + name;


return System.IO.File.GetCreationTime (PATH). ToString ("Yyyy-mm-dd HH:mm:ss");


}

private String Getcurrentpath ()
{
return string. IsNullOrEmpty (_current_path)? Server.MapPath ("~/"): _current_path;
}

Protected string GetIcon (string name)


{


String currentpath = Getcurrentpath ();


String path = Currentpath + name;


if (isfile (path))


{


int dotplace = name. LastIndexOf ('. ');


if (Dotplace &lt; 0)


{


Return "";


}


Else


{


return name. Substring (Dotplace + 1);


}


}


Else


{


Return "{DIR}";


}


}

Protected string GetSize (string name)
{
String currentpath = Getcurrentpath ();
String path = Currentpath + name;
if (isfile (path))
{
Long length = new System.IO.FileInfo (path). Length;
Return ((length/1024) + 1) + KB ("+ length + B)";
}
Else
{
return "Unknow";
}
}

Protected string Getupdatetime (string name)


{


String currentpath = Getcurrentpath ();


String path = Currentpath + name;


return System.IO.File.GetLastWriteTime (PATH). ToString ("Yyyy-mm-dd HH:mm:ss");


}





private void Initfiles ()


{


String currentpath = Getcurrentpath ();


string[] Directorys = System.IO.Directory.GetDirectories (Currentpath);


string[] files = System.IO.Directory.GetFiles (Currentpath);


system.collections.generic.ilist&lt;string&gt; arr = new system.collections.generic.list&lt;string&gt; ();


foreach (string s in Directorys)


{


Arr. ADD (S.replace (Currentpath, ""));


}


foreach (string s in files)


{


Arr. ADD (S.replace (Currentpath, ""));


}

Rptfile.datasource = arr;
Rptfile.databind ();
}

private bool Isfile (string path)
{
return System.IO.File.Exists (path);
}

private void Open (string fileName)
{
String currentpath = Getcurrentpath ();
String path = Currentpath + fileName;

if (isfile (path))


{


System.IO.FileInfo FileInfo = new System.IO.FileInfo (path);


Response.Clear ();


Response.clearcontent ();


Response.ClearHeaders ();


Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);


Response.AddHeader ("Content-length", fileInfo.Length.ToString ());


Response.AddHeader ("content-transfer-encoding", "binary");


Response.ContentType = "Application/octet-stream";


response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");


Response.WriteFile (Fileinfo.fullname);


Response.Flush ();


Response.End ();





}


Else


{


_current_path = PATH + "\";


Response.Redirect (Request.Url.AbsolutePath);


}


}





private void Root ()


{


_current_path = Server.MapPath ("~/");


Response.Redirect (Request.Url.AbsolutePath);


}





&lt;/script&gt;

<title></title>
<style type= "Text/css" >
body{margin:0; padding:10px background: #aaf; font-family:arial; font-size:14px;}

#container {border:1px #ddd solid}


#menu {background-color: #ccc;}


#menu. line{border-left:solid 1px #fff; width:0;}


#menu a{display:inline-block; margin:0; padding:8px 12px; text-decoration:none; }


#menu a:active, #menu a:hover{background-color: #ddd; }





#main {height:450px;}


#main. file{float:left; width:100px; height:60px; margin:8px; padding:5px; overflow:hidden; text-align:center;}


#main. file. icon{margin:0 5px; Font-family:impact,arial Black; font-size:30px; width:85px; height:40px; Overflow:hidden; }


#main. file. name{font-size:12px;}

#main, #upload_panel, #rename_panel {background-color: #fff; border:1px #ddd solid; border-left:0; border-right:0;}
#upload_panel, #rename_panel {padding:10px;}

#status_bar {background-color: #ccc;}
#status_bar span{display:inline-block margin:0; padding:5px 30px 5px 8px border-left:solid 1px #ddd;}
</style>
<script type= "Text/javascript" >
var Page = {Currentfile:null};

/* Page Load * *
function Pageload () {
Initmenu ();
Initfile ();
Initstatusbar ();
Initpanel ();
ShowMessage ();
}

/* Initialize Menu/*


function Initmenu () {


/* page loading events, handling initialization page operations.


var MenuItems = document.getElementById ("menu"). childnodes;


For (Var menu in MenuItems) {


if (menu &gt;= 0 &amp;&amp; (menuitems[menu].tagname + ""). toUpperCase () = = "A") {


var a = Menuitems[menu];


A.onclick = Clickmenuitem;


}


}


}

/* Initialize file list Event/*


function Initfile () {


var files = document.getElementById ("main"). ChildNodes;


For (var k in files) {


if (k &gt;= 0 &amp;&amp; (files[k].classname + ""). toLowerCase (). IndexOf ("file") &gt;= 0) {


var file = Files[k];


File.style.cursor = "pointer";


File.onclick = Clickfile;


}


}


}

/* Initialize the "Upload file" and "Modify filename" Module * *


function Initpanel () {


document.getElementById ("Upload_panel"). Style.display = "None"; Hide Upload file module


document.getElementById ("Rename_panel"). Style.display = "None"; Hide Modify filename Module


document.getElementById ("Fufile"). Value = ""; Empty upload file control


document.getElementById ("Txtnewname"). Value = ""; Empty Name text box


document.getElementById ("Hfoldname"). Value = ""; Empty Name text box


Document.getelementsbyname ("Btncancel") [0].onclick = Initpanel; Bind Cancel Button Click Event


Document.getelementsbyname ("Btncancel") [1].onclick = Initpanel; Bind Cancel Button Click Event


}

/* Initialize status bar/*


function Initstatusbar () {


var statusitems = document.getElementById ("Status_bar"). ChildNodes;


for (Var itemkey in Statusitems) {


if (itemkey &gt;= 0 &amp;&amp; (statusitems[itemkey].tagname + ""). toUpperCase () = "SPAN") {


var span = Statusitems[itemkey];


var value = Page.currentfile = null? "": Page.CurrentFile.getAttribute (Span.classname);


if ("filename" = = Span.className.toLowerCase ()) span.innerhtml = "FileName:" + value;


if ("type" = = Span.className.toLowerCase ()) span.innerhtml = "type:" + value;


if ("size" = = Span.className.toLowerCase ()) span.innerhtml = "Size:" + value;


if ("create_time" = = Span.className.toLowerCase ()) span.innerhtml = "Createtime:" + value;


if ("update_time" = = Span.className.toLowerCase ()) span.innerhtml = "LastUpdateTime:" + value;


}


}


}

/* Click menu item Event/*
function Clickmenuitem () {
Initpanel ();
var id = this.id;

Switch (ID) {


Case "Root":


Location.search = ' action=root ';


Break


Case "Back":


Location.search = ' Action=back ';


Break


Case "Open":


Open ();


Break


Case "NewFolder":


document.getElementById ("Rename_panel"). Style.display = "";


Break


Case "Upload":


document.getElementById ("Upload_panel"). Style.display = "";


Break


Case "Rename":


Rename ();


Break


Case "Delete":


Delete ()


Break


}

return false; Do not respond to hyperlink jump operations
}

/* Click File Event * *
function Clickfile () {
if (page.currentfile!= null) {
Page.CurrentFile.style.background = "";
}

            if (Page.currentfile = this) {
                 Page.CurrentFile.style.background = "";
                Page.currentfile = NULL;
           } else {
                 this.style.background = "#ddd";
                Page.currentfile = this;
           }
            Initstatusbar ();
            Initpanel ();
       }

function Delete () {
if (page.currentfile!= null) {
Location.search = ' action=delete&filename= ' + Page.CurrentFile.getAttribute ("FileName");
}
}

function Open () {
if (page.currentfile!= null) {
Location.search = ' action=open&filename= ' + Page.CurrentFile.getAttribute ("FileName");
}
}

function Rename () {
if (page.currentfile!= null) {
document.getElementById ("Txtnewname"). Value = Page.CurrentFile.getAttribute ("filename");
document.getElementById ("Hfoldname"). Value = Page.CurrentFile.getAttribute ("filename");
document.getElementById ("Rename_panel"). Style.display = "";
}
}

function ShowMessage () {


var msg = "&lt;%=msg%&gt;";


if (msg!= "") {


Alert (msg);


}


}


&lt;/script&gt;


&lt;/head&gt;


&lt;body onload= "pageload ()" &gt;


&lt;form id= "Form1" runat= "Server" &gt;


&lt;div id= "Container" &gt;


&lt;div id= "Menu" &gt;


&lt;a href= "#" id= "Root" &gt;Root&lt;/a&gt;


&lt;a href= "#" id= "Back" &gt;Back&lt;/a&gt;


&lt;a href= "#" id= "Open" &gt;open (Download) &lt;/a&gt;


&lt;a class= "line" &gt;&lt;/a&gt;


&lt;a href= "#" id= "NewFolder" &gt;NewFolder&lt;/a&gt;


&lt;a href= "#" id= "Upload" &gt;Upload&lt;/a&gt;


&lt;a href= "#" id= "Rename" &gt;Rename&lt;/a&gt;


&lt;a href= "#" id= "Delete" &gt;Delete&lt;/a&gt;


&lt;/div&gt;


&lt;div id= "Main" &gt;


&lt;asp:repeater id= "Rptfile" runat= "Server" &gt;


&lt;ItemTemplate&gt;


&lt;div class= "File"


Filename= "&lt;%# Container.DataItem%&gt;"


Size= "&lt;%# getsize (Container.DataItem +" ")%&gt;"


Type= "&lt;%# GetIcon (Container.DataItem +" ")%&gt;"


Create_time= "&lt;%# getcreatetime (Container.DataItem +" ")%&gt;"


Update_time= "&lt;%# getcreatetime (Container.DataItem +" ")%&gt;" &gt;


&lt;div class= "icon" &gt;&lt;%# GetIcon (Container.DataItem + "")%&gt;&lt;/div&gt;


&lt;div class= "name" &gt;&lt;%# Container.DataItem%&gt;&lt;/div&gt;


&lt;/div&gt;


&lt;/ItemTemplate&gt;


&lt;/asp:Repeater&gt;


&lt;div style= "Clear:both;" &gt;&lt;/div&gt;


&lt;/div&gt;


&lt;div id= "Upload_panel" &gt;


&lt;asp:fileupload id= "Fufile" runat= "Server"/&gt;&amp;nbsp;


&lt;asp:radiobutton id= "Rbcover" runat= "Server" Groupname= "Filereapter" text= "Cover" checked= "true"/&gt;


&lt;asp:radiobutton id= "Rbrename" runat= "Server" Groupname= "Filereapter" text= "Rename"/&gt;&amp;nbsp;


&lt;asp:button id= "Btnupload" runat= "Server" text= "Upload" onclick= "Btnupload_click"/&gt;&amp;nbsp;


&lt;input name= "btncancel" type= "button" value= "Cancel"/&gt;


&lt;/div&gt;


&lt;div id= "Rename_panel" &gt;


&lt;asp:textbox id= "Txtnewname" runat= "Server" text= "ASDF" &gt;&lt;/asp:TextBox&gt;&amp;nbsp;


&lt;asp:button id= "btnsave" runat= "Server" text= "Save" onclick= "Btnsave_click"/&gt;&amp;nbsp;


&lt;input name= "btncancel" type= "button" value= "Cancel"/&gt;


&lt;asp:hiddenfield id= "Hfoldname" runat= "Server"/&gt;


&lt;/div&gt;


&lt;div id= "Status_bar" &gt;


&lt;span class= "filename" &gt; name:&lt;/span&gt;


&lt;span class= "type" &gt; Type:&lt;/span&gt;


&lt;span class= "Size" &gt; Size:&lt;/span&gt;


&lt;span class= "Create_time" &gt; Creation time:&lt;/span&gt;


&lt;span class= "Update_time" &gt; Modification Time:&lt;/span&gt;


&lt;/div&gt;


&lt;/div&gt;


&lt;/form&gt;


&lt;/body&gt;


&lt;/html&gt;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.