一个简单的asp.net 管理Web站点文件的页面程序

作者:袖梨 2022-06-25

先看效果

WebFileManager

代码如下 复制代码


C#" %>

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 {
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 arr = new System.Collections.Generic.List();
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);
}














filename=""
size=""
type=""
create_time=""
update_time="">















TextBox ID="txtNewName" runat="server" Text="asdf">





名称:
类型:
大小:
创建时间:
修改时间:




相关文章

精彩推荐