先看效果
WebFileManager
代码如下
<%@ Page Language="C#" %>
<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
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>
<script type="text/javascript">
var Page = { CurrentFile: null };
/* 页面加载 */
function PageLoad() {
InitMenu();
InitFile();
InitStatusBar();
InitPanel();
ShowMessage();
}
/* 初始化菜单 */
function InitMenu() {
/* 页面加载事件,处理初始化页面操作 */
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;
}
}
}
/* 初始化文件列表事件 */
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;
}
}
}
/* 初始化“上传文件”和“修改文件名”模块 */
function InitPanel() {
document.getElementById("upload_panel").style.display = "none"; //隐藏上传文件模块
document.getElementById("rename_panel").style.display = "none"; //隐藏修改文件名模块
document.getElementById("fuFile").value = ""; //清空上传文件控件
document.getElementById("txtNewName").value = ""; //清空名称文本框
document.getElementById("hfOldName").value = ""; //清空名称文本框
document.getElementsByName("btnCancel")[0].onclick = InitPanel; //绑定 Cancel 按钮 Click 事件
document.getElementsByName("btnCancel")[1].onclick = InitPanel; //绑定 Cancel 按钮 Click 事件
}
/* 初始化状态栏 */
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;
}
}
}
/* 单击菜单项事件 */
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; //不响应超链接跳转操作
}
/* 单击文件事件 */
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>
简单是一种方法|一个简单的asp.net 管理Web站点文件的页面程序
http://m.bbyears.com/wangyetexiao/34821.html
推荐访问: