<!-- 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 "> <script runat= "Server" >
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 ("~/")) >= 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 < 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<string> arr = new system.collections.generic.list<string> ();
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);
}
</script> <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 >= 0 && (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 >= 0 && (files[k].classname + ""). toLowerCase (). IndexOf ("file") >= 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 >= 0 && (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 = "<%=msg%>";
if (msg!= "") {
Alert (msg);
}
}
</script>
</head>
<body onload= "pageload ()" >
<form id= "Form1" runat= "Server" >
<div id= "Container" >
<div id= "Menu" >
<a href= "#" id= "Root" >Root</a>
<a href= "#" id= "Back" >Back</a>
<a href= "#" id= "Open" >open (Download) </a>
<a class= "line" ></a>
<a href= "#" id= "NewFolder" >NewFolder</a>
<a href= "#" id= "Upload" >Upload</a>
<a href= "#" id= "Rename" >Rename</a>
<a href= "#" id= "Delete" >Delete</a>
</div>
<div id= "Main" >
<asp:repeater id= "Rptfile" runat= "Server" >
<ItemTemplate>
<div class= "File"
Filename= "<%# Container.DataItem%>"
Size= "<%# getsize (Container.DataItem +" ")%>"
Type= "<%# GetIcon (Container.DataItem +" ")%>"
Create_time= "<%# getcreatetime (Container.DataItem +" ")%>"
Update_time= "<%# getcreatetime (Container.DataItem +" ")%>" >
<div class= "icon" ><%# GetIcon (Container.DataItem + "")%></div>
<div class= "name" ><%# Container.DataItem%></div>
</div>
</ItemTemplate>
</asp:Repeater>
<div style= "Clear:both;" ></div>
</div>
<div id= "Upload_panel" >
<asp:fileupload id= "Fufile" runat= "Server"/>&nbsp;
<asp:radiobutton id= "Rbcover" runat= "Server" Groupname= "Filereapter" text= "Cover" checked= "true"/>
<asp:radiobutton id= "Rbrename" runat= "Server" Groupname= "Filereapter" text= "Rename"/>&nbsp;
<asp:button id= "Btnupload" runat= "Server" text= "Upload" onclick= "Btnupload_click"/>&nbsp;
<input name= "btncancel" type= "button" value= "Cancel"/>
</div>
<div id= "Rename_panel" >
<asp:textbox id= "Txtnewname" runat= "Server" text= "ASDF" ></asp:TextBox>&nbsp;
<asp:button id= "btnsave" runat= "Server" text= "Save" onclick= "Btnsave_click"/>&nbsp;
<input name= "btncancel" type= "button" value= "Cancel"/>
<asp:hiddenfield id= "Hfoldname" runat= "Server"/>
</div>
<div id= "Status_bar" >
<span class= "filename" > name:</span>
<span class= "type" > Type:</span>
<span class= "Size" > Size:</span>
<span class= "Create_time" > Creation time:</span>
<span class= "Update_time" > Modification Time:</span>
</div>
</div>
</form>
</body>
</html> |