温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:小范企业管理源码
当前文件:
XiaoFanWeb/HelperScripts/ftb.imagegallery.aspx,打开代码结构图
XiaoFanWeb/HelperScripts/ftb.imagegallery.aspx,打开代码结构图1<%@ Page language="c#" %> 2
<script runat="server"> 3
4
// Messages 5
private string NoFileMessage = "您没有选择文件。"; 6
private string UploadSuccessMessage = "上传成功"; 7
private string UploadFailureMessage = "上传失败。"; 8
private string NoImagesMessage = "该文件夹不存在或者是空的"; 9
private string NoFolderSpecifiedMessage = "您要上传到的文件夹不存在。"; 10
private string NoFileToDeleteMessage = "您没有选中要删除的文件。"; 11
private string InvalidFileTypeMessage = "您无法上传这种类型的文件。"; 12
private string[] AcceptedFileTypes = new string[] {"jpg","jpeg","jpe","gif","png"}; 13
14
// Configuration 15
private bool UploadIsEnabled = true; // 是否允许上传文件 16
private bool DeleteIsEnabled = true; // 是否允许删除文件 17
private string DefaultImageFolder = "images"; // 默认的起始文件夹 18
19
private void Page_Load(object sender, System.EventArgs e) { 20
string isframe = "" + Request["frame"]; 21
22
if (isframe != "") { 23
MainPage.Visible = true; 24
iframePanel.Visible = false; 25
26
string rif = "" + Request["rif"]; 27
string cif = "" + Request["cif"]; 28
29
if (cif != "" && rif != "") { 30
RootImagesFolder.Value = rif; 31
CurrentImagesFolder.Value = cif; 32
} else { 33
RootImagesFolder.Value = DefaultImageFolder; 34
CurrentImagesFolder.Value = DefaultImageFolder; 35
} 36
37
UploadPanel.Visible = UploadIsEnabled; 38
DeleteImage.Visible = DeleteIsEnabled; 39
40
string FileErrorMessage = ""; 41
string ValidationString = ".*("; 42
//[\.jpg]|[\.jpeg]|[\.jpe]|[\.gif]|[\.png])$" 43
for (int i=0;i<AcceptedFileTypes.Length; i++) { 44
ValidationString += "[\\." + AcceptedFileTypes[i] + "]"; 45
if (i < (AcceptedFileTypes.Length-1)) ValidationString += "|"; 46
FileErrorMessage += AcceptedFileTypes[i]; 47
if (i < (AcceptedFileTypes.Length-1)) FileErrorMessage += ", "; 48
} 49
FileValidator.ValidationExpression = ValidationString+")$"; 50
FileValidator.ErrorMessage=FileErrorMessage; 51
52
if (!IsPostBack) { 53
DisplayImages(); 54
} 55
} else { 56
57
} 58
} 59
60
public void UploadImage_OnClick(object sender, EventArgs e) { 61
if (Page.IsValid) { 62
if (CurrentImagesFolder.Value != "") { 63
if (UploadFile.PostedFile.FileName.Trim() != "") { 64
if (IsValidFileType(UploadFile.PostedFile.FileName)) { 65
try { 66
string UploadFileName = ""; 67
string UploadFileDestination = ""; 68
UploadFileName = UploadFile.PostedFile.FileName; 69
UploadFileName = UploadFileName.Substring(UploadFileName.LastIndexOf("\\")+1); 70
UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath; 71
UploadFileDestination += CurrentImagesFolder.Value; 72
UploadFileDestination += "\\"; 73
UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName); 74
ResultsMessage.Text = UploadSuccessMessage; 75
} catch(Exception ex) { 76
//ResultsMessage.Text = "Your file could not be uploaded: " + ex.Message; 77
ResultsMessage.Text = UploadFailureMessage; 78
} 79
} else { 80
ResultsMessage.Text = InvalidFileTypeMessage; 81
} 82
} else { 83
ResultsMessage.Text = NoFileMessage; 84
} 85
} else { 86
ResultsMessage.Text = NoFolderSpecifiedMessage; 87
} 88
} else { 89
ResultsMessage.Text = InvalidFileTypeMessage; 90
91
} 92
DisplayImages(); 93
} 94
95
public void DeleteImage_OnClick(object sender, EventArgs e) { 96
if (FileToDelete.Value != "" && FileToDelete.Value != "undefined") { 97
try { 98
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath; 99
System.IO.File.Delete(AppPath + CurrentImagesFolder.Value + "\\" + FileToDelete.Value); 100
ResultsMessage.Text = "已删除: " + FileToDelete.Value; 101
} catch(Exception ex) { 102
ResultsMessage.Text = "删除失败。"; 103
} 104
} else { 105
ResultsMessage.Text = NoFileToDeleteMessage; 106
} 107
DisplayImages(); 108
} 109
110
private bool IsValidFileType(string FileName) { 111
string ext = FileName.Substring(FileName.LastIndexOf(".")+1,FileName.Length-FileName.LastIndexOf(".")-1); 112
for (int i=0; i<AcceptedFileTypes.Length; i++) { 113
if (ext == AcceptedFileTypes[i]) { 114
return true; 115
116
} 117
} 118
return false; 119
} 120
121
122
private string[] ReturnFilesArray() { 123
if (CurrentImagesFolder.Value != "") { 124
try { 125
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath; 126
string ImageFolderPath = AppPath + CurrentImagesFolder.Value; 127
string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*"); 128
return FilesArray; 129
130
131
} catch { 132
133
return null; 134
} 135
} else { 136
return null; 137
} 138
139
} 140
141
private string[] ReturnDirectoriesArray() { 142
if (CurrentImagesFolder.Value != "") { 143
try { 144
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath; 145
string CurrentFolderPath = AppPath + CurrentImagesFolder.Value; 146
string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*"); 147
return DirectoriesArray ; 148
} catch { 149
return null; 150
} 151
} else { 152
return null; 153
} 154
} 155
156
public void DisplayImages() { 157
string[] FilesArray = ReturnFilesArray(); 158
string[] DirectoriesArray = ReturnDirectoriesArray(); 159
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath; 160
string AppUrl; 161
162
//Get the application's URL 163
if (Request.ApplicationPath == "/") 164
AppUrl = Request.ApplicationPath; 165
else 166
AppUrl = Request.ApplicationPath + "/"; 167
168
GalleryPanel.Controls.Clear(); 169
if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) { 170
gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value; 171
} else { 172
string ImageFileName = ""; 173
string ImageFileLocation = ""; 174
175
int thumbWidth = 94; 176
int thumbHeight = 94; 177
178
if (CurrentImagesFolder.Value != RootImagesFolder.Value) { 179
180
System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage(); 181
myHtmlImage.Src = AppUrl + "images/ftb/folder.up.gif"; 182
myHtmlImage.Attributes["unselectable"]="on"; 183
myHtmlImage.Attributes["align"]="absmiddle"; 184
myHtmlImage.Attributes["vspace"]="36"; 185
186
string ParentFolder = CurrentImagesFolder.Value.Substring(0,CurrentImagesFolder.Value.LastIndexOf("\\")); 187
188
System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel(); 189




private string[] AcceptedFileTypes
}