解决方案 »
- asp.net 问题 ,Application用法
- 如果对一个aspx文件页面的内容进行替换
- 求一个ASP.NET2.0+SQLServer2000或2005开发的开源的博客系统
- DataGrid的使用方法!(复杂应用)
- web 应用程序修改数据库的问题
- 紧急求救
- 一段简单的连接数据库,并显示数据表的程序,不知道错在哪儿?
- 网站发布以后,不能加载js文件
- sqlserver 数据库访问问题〔链接池〕
- 将null值插入SQL Server的Datetime字段的问题!谁有好的方法??
- 在一个aspx页中,有个Image控件,我要在当前页里把一个Bitmap类的实例让Image显示出来,应该怎么做???
- 汉字的问题
把代码拷到aspx文件中,放到虚拟目录下,
然后在虚拟目录中建立Images目录,下面放4个图片:
file.unrecognizable.gif
folder.big.gif
folder.small.gif
folder.up.gif
。
然后浏览之:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>File Maintain System</title>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<base target=_self />
<script runat="server"> //Messages
private string noFileMessage = "You must select a file!";
private string uploadSuccessMessage = "Upload success!";
private string uploadFailureMessage = "Upload failed!";
private string noImagesMessage = "The folder is not exist or it is empty.";
private string noFolderSpecifiedMessage = "The target folder is not exist.";
private string noFileToDeleteMessage = "You must select a file to delete.";
private string noFolderToDeleteMessage = "You must select a folder to delete.";
private string invalidFileTypeMessage = "The type of the file you upload is not allowed.";
private string validFileTypePromptMessage = "Allowed filetype is {0}";//
private string noFolderNameMessage = "You must provide a folder name!"; private string acceptedFileTypes = "*.*;";
private string acceptedImageFileTypes = "*.jpg|*.jpeg|*.jpe|*.gif|*.png|*.bmp;"; //Configuration
private bool uploadIsEnabled = true; //whether allow upload file.
private bool deleteIsEnabled = true; //whether allow delete file.
private string rootFolder = "userdata"; //Initial folder.
private string validFileTypeString = "Allowed filetype is {0}"; private void Page_Load(object sender, System.EventArgs e)
{
try
{
CurrentFolder.Value = "" + Request["folder"]; string userRoot =
System.IO.Path.Combine
(
HttpContext.Current.Request.PhysicalApplicationPath,
rootFolder + "\\" + this.Context.User.Identity.Name
); string currentPath =
System.IO.Path.GetFullPath
(
System.IO.Path.Combine
(
userRoot,
CurrentFolder.Value
)
); btnDeleteFile.Attributes.Add("onclick", "return confirm('" + "Are u sure you want to delete the selected file(s)?" + "');");
btnDeleteFolder.Attributes.Add("onclick", "return confirm('" + "Are u sure you want to delete the selected folder(s)?" + "');"); FileValidator.ValidationExpression = string.Format(@"({0})$", System.Text.RegularExpressions.Regex.Replace(acceptedFileTypes, @";.*", string.Empty).Replace(".", "\\.").Replace("*", ".*"));
FileValidator.ErrorMessage = validFileTypeString; if(!System.IO.Directory.Exists(userRoot))
{
System.IO.Directory.CreateDirectory(userRoot);
} if(!IsPostBack)
{
DisplayFiles();
}
}
catch(System.Exception)// ex)
{
//MessageBox.Show(ex.Message);
}
} public void btnUpload_OnClick(object sender, EventArgs e)
{
if(Page.IsValid)
{
if(UploadFile.PostedFile.FileName.Trim() != "")
{
if(IsValidFileType(UploadFile.PostedFile.FileName))
{
try
{
string uploadFileName = "";
string uploadFileDestination = "";
uploadFileName = UploadFile.PostedFile.FileName;
uploadFileName = uploadFileName.Substring(uploadFileName.LastIndexOf("\\")+1);
uploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
uploadFileDestination += this.rootFolder + "\\" + this.Context.User.Identity.Name + "\\" + CurrentFolder.Value;
uploadFileDestination += "\\";
UploadFile.PostedFile.SaveAs(uploadFileDestination + uploadFileName);
resultsMessage.Text = uploadSuccessMessage;
}
catch(System.Exception)
{
//resultsMessage.Text = "Your file could not be uploaded: " + ex.Message;
resultsMessage.Text = uploadFailureMessage;
}
}
else
{
resultsMessage.Text = invalidFileTypeMessage + "\n" + validFileTypeString;
}
}
else
{
resultsMessage.Text = noFileMessage;
}
}
else
{
resultsMessage.Text = invalidFileTypeMessage; }
DisplayFiles();
}
public void btnDeleteFile_OnClick(object sender, EventArgs e)
{
if(FileToDelete.Value != "" && FileToDelete.Value != "undefined")
{
try
{
string appPath = HttpContext.Current.Request.PhysicalApplicationPath;
System.IO.File.Delete(appPath + this.rootFolder + "\\" + this.Context.User.Identity.Name + "\\" + CurrentFolder.Value + "\\" + FileToDelete.Value);
resultsMessage.Text = FileToDelete.Value + " was deleted";
}
catch(System.Exception ex)
{
resultsMessage.Text = "Delete failed." + this.Server.HtmlDecode(ex.Message);
}
}
else
{
resultsMessage.Text = noFileToDeleteMessage;
} DisplayFiles();
} public void btnCreateFolder_OnClick(object sender, EventArgs e)
{
if(txtDirectory.Value != "" && txtDirectory.Value != "undefined")
{
try
{
string folderName = txtDirectory.Value;
string appPath = HttpContext.Current.Request.PhysicalApplicationPath;
string fullPath = appPath + this.rootFolder + "\\" + this.Context.User.Identity.Name + "\\" + CurrentFolder.Value + "\\" + folderName; System.IO.Directory.CreateDirectory(fullPath); resultsMessage.Text = (appPath + this.rootFolder + "\\" + this.Context.User.Identity.Name + "\\" + CurrentFolder.Value + "\\" + folderName + " was created.").Replace("\\\\", "\\");
}
catch(System.Exception)
{
resultsMessage.Text = "Create failed.";
}
}
else
{
resultsMessage.Text = noFolderNameMessage;
}
DisplayFiles();
}
public void btnDeleteFolder_OnClick(object sender, EventArgs e)
{
if(FolderToDelete.Value != "" && FolderToDelete.Value != "undefined")
{
try
{
string appPath = HttpContext.Current.Request.PhysicalApplicationPath;
string fullPath = appPath + this.rootFolder + "\\" + this.Context.User.Identity.Name + "\\" + CurrentFolder.Value + "\\" + FolderToDelete.Value; System.IO.Directory.Delete(fullPath, true); resultsMessage.Text = fullPath + " was deleted";
}
catch(System.Exception)
{
resultsMessage.Text = "Delete failed.";
}
}
else
{
resultsMessage.Text = noFolderToDeleteMessage;
}
DisplayFiles();
} private bool IsValidFileType(string fileName)
{
if(acceptedFileTypes.IndexOf(".*") != -1)
{
return true;
}
else
{
System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
if(acceptedFileTypes.IndexOf(fi.Extension.ToLower()) != -1)
{
return true;
}
} return false;
}
private string[] GetFilesArray()
{
try
{
string folderPath =
System.IO.Path.Combine
(
System.IO.Path.Combine
(
HttpContext.Current.Request.PhysicalApplicationPath,
rootFolder + "\\" + this.Context.User.Identity.Name
),
CurrentFolder.Value
); string[] filesArray = System.IO.Directory.GetFiles(folderPath, "*"); return filesArray;
}
catch(System.Exception)
{
return null;
}
} private string[] GetDirectoriesArray()
{
try
{
string folderPath =
System.IO.Path.Combine
(
System.IO.Path.Combine
(
HttpContext.Current.Request.PhysicalApplicationPath,
rootFolder + "\\" + this.Context.User.Identity.Name
),
CurrentFolder.Value
); string[] directoriesArray = System.IO.Directory.GetDirectories(folderPath, "*");
return directoriesArray;
}
catch(System.Exception)
{
return null;
}
}