温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net文件在线管理系统源码
当前文件:
WebFiles/Default.aspx.cs,打开代码结构图
WebFiles/Default.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
//Email:jetaimefj@163.com 11
//该源码下载自www.51aspx.com(51aspx.com) 12
using System.IO; 13
public partial class _Default : System.Web.UI.Page 14
...{ 15
protected void Page_Load(object sender, EventArgs e) 16
...{ 17
if (!Page.IsPostBack) 18
...{ 19
//初始化文件夹信息 20
InitFolderInfo(); 21
//初始化上传限制信息 22
InitUploadLimit(); 23
//初始化列表框控件文件列表信息 24
InitFileList(); 25
} 26
} 27
初始化文件夹信息#region 初始化文件夹信息 28
private void InitFolderInfo() 29
...{ 30
//从config中读取文件上传路径 31
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 32
//如果上传文件夹不存在,则根据config创建一个 33
if(!Directory.Exists(Server.MapPath(strFileUpladPath))) 34
...{ 35
Directory.CreateDirectory(Server.MapPath(strFileUpladPath)); 36
} 37
//将虚拟路径转换为物理路径 38
string strFilePath = Server.MapPath(strFileUpladPath); 39
//从config里读取文件夹容量限制 40
double iFolderSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FolderSizeLimit"]); 41
//声明文件夹已经使用的容量 42
double iFolderCurrentSize = 0; 43
//获取文件夹中的所有文件 44
FileInfo[] arrFiles = new DirectoryInfo(strFilePath).GetFiles(); 45
//循环文件获已经使用的容量 46
foreach (FileInfo fi in arrFiles) 47
...{ 48
iFolderCurrentSize += Convert.ToInt32(fi.Length / 1024); 49
} 50
第二种获得文件夹使用大小的方法#region 第二种获得文件夹使用大小的方法 51
//DirectoryInfo dir = new DirectoryInfo(strFilePath); 52
//foreach (FileSystemInfo fi in dir.GetFileSystemInfos()) 53
//{ 54
// FileInfo finf = new FileInfo(fi.FullName); 55
// iFolderCurrentSize += Convert.ToInt32(finf.Length / 1024); 56
//} 57
#endregion 58
//把文件夹容量和以用文件夹容量赋值给标签 59
lbl_FolderInfo.Text = string.Format("文件夹容量限制:{0}M,已用容量:{1}KB", iFolderSizeLimit / 1024, iFolderCurrentSize); 60
} 61
#endregion 62
初始化上传限制信息#region 初始化上传限制信息 63
private void InitUploadLimit() 64
...{ 65
//从config中读取上传文件夹类型限制并根据逗号分割成字符串数组 66
string[] arrFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString().Split(','); 67
//从config中读取上传文件大小限制 68
double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]); 69
//遍历字符串数组把所有项加入项目编号控件 70
for (int i = 0; i < arrFileTypeLimit.Length; i++) 71
...{ 72
bl_TileTypeLimit.Items.Add(arrFileTypeLimit[i].ToString()); 73
} 74
//把文件大小限制赋值给标签 75
lab_FileSizeLimit.Text = string.Format("{0:f2}M", iFileSizeLimit / 1024); 76
} 77
#endregion 78
初始化列表框控件文件列表信息#region 初始化列表框控件文件列表信息 79
private void InitFileList() 80
...{ 81
//从config中获取文件上传路径 82
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 83
//将虚拟路径转换为物理路径 84
string strFilePath = Server.MapPath(strFileUpladPath); 85
//读取上传文件夹下所有文件 86
FileInfo[] arrFile = new DirectoryInfo(strFilePath).GetFiles(); 87
//把文件名逐一添加到列表框控件 88
foreach(FileInfo fi in arrFile) 89
...{ 90
lb_FileList.Items.Add(fi.Name); 91
} 92
} 93
#endregion 94
判断文件大小限制#region 判断文件大小限制 95
private bool IsAllowableFileSize() 96
...{ 97
//从web.config读取判断文件大小的限制 98
double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]) * 1024; 99
//判断文件是否超出了限制 100
if (iFileSizeLimit > FileUpload.PostedFile.ContentLength) 101
...{ 102
return true; 103
} 104
else 105
...{ 106
return false; 107
} 108
} 109
#endregion 110
判断文件类型限制#region 判断文件类型限制 111
protected bool IsAllowableFileType() 112
...{ 113
//从web.config读取判断文件类型限制 114
string strFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString(); 115
//当前文件扩展名是否包含在这个字符串中 116
if(strFileTypeLimit.IndexOf(Path.GetExtension(FileUpload.FileName).ToLower()) >0) 117
return true; 118
else 119
return false; 120
} 121
#endregion 122
弹出警告消息#region 弹出警告消息 123
protected void ShowMessageBox(string strMessage) 124
...{ 125
Response.Write(string.Format("<script>alert('{0}')</script>",strMessage)); 126
} 127
#endregion 128
上传文件按钮事件#region 上传文件按钮事件 129
protected void btn_Upload_Click(object sender, EventArgs e) 130
...{ 131
//判断用户是否选择了文件 132
if (FileUpload.HasFile) 133
...{ 134
//调用自定义方法判断文件类型否符合 135
if (IsAllowableFileType()) 136
...{ 137
//判断文件大小是否符合 138
if (IsAllowableFileSize()) 139
...{ 140
//从web.config中读取上传路径 141
string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 142
//从UploadFile控件中读取文件名 143
string strFileName = FileUpload.FileName; 144
//组合成物理路径 145
string strFilePhysicalPath = Server.MapPath(strFileUploadPath + "/") + strFileName; 146
//判断文件是否存在 147
if(!File.Exists(strFilePhysicalPath)) 148
...{ 149
//保存文件 150
FileUpload.SaveAs(strFilePhysicalPath); 151
//更新列表框 152
lb_FileList.Items.Add(strFileName); 153
//更新文件夹信息 154
InitFolderInfo(); 155
ShowMessageBox("上传成功!"); 156
} 157
else 158
...{ 159
ShowMessageBox("文件已经存在!"); 160
} 161
} 162
else 163
...{ 164
ShowMessageBox("文件大小不符合要求!"); 165
} 166
} 167
else 168
...{ 169
ShowMessageBox("类型不匹配"); 170
} 171
} 172
} 173
#endregion 174
列表框事件#region 列表框事件 175
protected void lb_FileList_SelectedIndexChanged(object sender, EventArgs e) 176
...{ 177
//从config中读取文件上传路径 178
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 179
//从列表框中读取选择的文件名 180
string strFileName = lb_FileList.SelectedValue; 181
//组合成物理路径 182
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName; 183
//根据物理路径实例化文件信息类 184
FileInfo fi = new FileInfo(strFilePhysicalPath); 185
//或得文件大小和创建日期赋值给标签 186
lbl_FileDescription.Text = string.Format("文件大小:{0}字节<br><br>上传时间:{1}<br>", fi.Length, fi.CreationTime); 187
//把文件名赋值给重命名文件框 188
tb_FileNewName.Text = strFileName; 189
} 190
#endregion 191
下载文件按钮事件#region 下载文件按钮事件 192
protected void btn_DownLoad_Click(object sender, EventArgs e) 193
...{ 194
//从web.config读取文件上传路径 195
string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToLower(); 196
//从列表框中读取选择的文件 197
string strFileName = lb_FileList.SelectedValue; 198
//组合成物理路径 199
string FullFileName = Server.MapPath(strFileUploadPath + "/") + strFileName; 200
201
FileInfo DownloadFile = new FileInfo(FullFileName); 202
Response.Clear(); 203
Response.ClearHeaders(); 204
Response.Buffer = false; 205
Response.ContentType = "application/octet-stream "; 206
Response.AppendHeader("Content-Disposition ", "attachment;filename= " 207
+ HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); 208
Response.AppendHeader("Content-Length ", DownloadFile.Length.ToString()); 209
Response.WriteFile(DownloadFile.FullName); 210
Response.Flush(); 211
Response.End(); 212
} 213
#endregion 214
删除文件#region 删除文件 215
protected void btn_Delete_Click(object sender, EventArgs e) 216
...{ 217
//从config中读取文件上传路径 218
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 219
//从列表框中读取选择的文件名 220
string strFileName = lb_FileList.SelectedValue; 221
//组合成物理路径 222
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName; 223
//删除文件 224
System.IO.File.Delete(strFilePhysicalPath); 225
//更新文件列表框控件 226
lb_FileList.Items.Remove(lb_FileList.Items.FindByText(strFileName)); 227
//更新文件夹信息 228
InitFolderInfo(); 229
//更新文件描述信息 230
tb_FileNewName.Text = ""; 231
//更新重命名文本框 232
lbl_FileDescription.Text = ""; 233
//调用自定义消息提示 234
ShowMessageBox("删除成功!"); 235
} 236
#endregion 237
重命名文件#region 重命名文件 238
protected void btn_Rename_Click(object sender, EventArgs e) 239
...{ 240
//从web.config中读取文件上传路径 241
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString(); 242
//从列表框中控件中读取选择的文件名 243
string strFileName = lb_FileList.SelectedValue; 244
//重命名文本框或得选择的文件名 245
string strFileNewName = tb_FileNewName.Text; 246
//组合成物理路径 247
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName; 248
//组合成新物理路径 249
string strFileNewPhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileNewName; 250
//文件重命名,即获取新地址覆盖旧地址的过程 251
System.IO.File.Move(strFilePhysicalPath, strFileNewPhysicalPath); 252
//找到文件列表的匹配项 253
ListItem li = lb_FileList.Items.FindByText(strFileName); 254
//修改文字 255
li.Text = strFileNewName; 256
//修改值 257
li.Value = strFileNewName; 258
//调用自定义方法现实 259
ShowMessageBox("文件覆盖成功!"); 260
} 261
#endregion 262
下拉列表事件#region 下拉列表事件 263
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 264
...{ 265
MultiView1.ActiveViewIndex = Convert.ToInt32(DropDownList1.SelectedValue); 266
} 267
#endregion 268
} 269





}
}