ftb.imagegallery.aspx
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:14k
源码类别:

OA系统

开发平台:

C#

  1. <%@ Page language="c#" %>
  2. <script language="C#" runat="server">
  3. // Messages
  4. private string NoFileMessage = "未选择文件";
  5. private string UploadSuccessMessage = "上传成功";
  6. private string NoImagesMessage = "没有图片";
  7. private string NoFolderSpecifiedMessage = "目录不存在";
  8. private string NoFileToDeleteMessage = "未删除文件";
  9. private string InvalidFileTypeMessage = "无效的文件类型";
  10. private string[] AcceptedFileTypes = new string[] {"jpg","jpeg","jpe","gif","bmp","png"};
  11. // Configuration
  12. private bool UploadIsEnabled = true;
  13. private bool DeleteIsEnabled = true;
  14. private string DefaultImageFolder = "OtherImages";
  15. private void Page_Load(object sender, System.EventArgs e) {
  16. string isframe = "" + Request["frame"];
  17. if (isframe != "") {
  18. MainPage.Visible = true;
  19. iframePanel.Visible = false;
  20. string rif = "" + Request["rif"];
  21. string cif = "" + Request["cif"];
  22. if (cif != "" && rif != "") {
  23. RootImagesFolder.Value = rif;
  24. CurrentImagesFolder.Value = cif;
  25. } else {
  26. RootImagesFolder.Value = DefaultImageFolder;
  27. CurrentImagesFolder.Value = DefaultImageFolder;
  28. }
  29. UploadPanel.Visible = UploadIsEnabled;
  30. DeleteImage.Visible = DeleteIsEnabled;
  31. string FileErrorMessage = "";
  32. string ValidationString = ".*(";
  33. //[.jpg]|[.jpeg]|[.jpe]|[.gif]|[.bmp]|[.png])$"
  34. for (int i=0;i<AcceptedFileTypes.Length; i++) {
  35. ValidationString += "[\." + AcceptedFileTypes[i] + "]";
  36. if (i < (AcceptedFileTypes.Length-1)) ValidationString += "|";
  37. FileErrorMessage += AcceptedFileTypes[i];
  38. if (i < (AcceptedFileTypes.Length-1)) FileErrorMessage += ", ";
  39. }
  40. FileValidator.ValidationExpression = ValidationString+")$";
  41. FileValidator.ErrorMessage=FileErrorMessage;
  42. if (!IsPostBack) {
  43. DisplayImages();
  44. }
  45. } else {
  46. }
  47. }
  48. public void UploadImage_OnClick(object sender, EventArgs e) {
  49. if (Page.IsValid) {
  50. if (CurrentImagesFolder.Value != "") {
  51. if (UploadFile.PostedFile.FileName.Trim() != "") {
  52. if (IsValidFileType(UploadFile.PostedFile.FileName)) {
  53. try {
  54. string UploadFileName = "";
  55. string UploadFileDestination = "";
  56. UploadFileName = UploadFile.PostedFile.FileName;
  57. UploadFileName = UploadFileName.Substring(UploadFileName.LastIndexOf("\")+1);
  58. UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
  59. UploadFileDestination += CurrentImagesFolder.Value;
  60. UploadFileDestination += "\";
  61. UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName);
  62. ResultsMessage.Text = UploadSuccessMessage;
  63. } catch(Exception ex) {
  64. //ResultsMessage.Text = "Your file could not be uploaded: " + ex.Message;
  65. ResultsMessage.Text = "There was an error.";
  66. }
  67. } else {
  68. ResultsMessage.Text = InvalidFileTypeMessage;
  69. }
  70. } else {
  71. ResultsMessage.Text = NoFileMessage;
  72. }
  73. } else {
  74. ResultsMessage.Text = NoFolderSpecifiedMessage;
  75. }
  76. } else {
  77. ResultsMessage.Text = InvalidFileTypeMessage;
  78. }
  79. DisplayImages();
  80. }
  81. public void DeleteImage_OnClick(object sender, EventArgs e) {
  82. if (FileToDelete.Value != "" && FileToDelete.Value != "undefined") {
  83. try {
  84. string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
  85. System.IO.File.Delete(AppPath  + CurrentImagesFolder.Value + "\" + FileToDelete.Value);
  86. ResultsMessage.Text = "已删除: " + FileToDelete.Value;
  87. } catch(Exception ex) {
  88. ResultsMessage.Text = "出现错误.";
  89. }
  90. } else {
  91. ResultsMessage.Text = NoFileToDeleteMessage;
  92. }
  93. DisplayImages();
  94. }
  95. private bool IsValidFileType(string FileName) {
  96. string ext = FileName.Substring(FileName.LastIndexOf(".")+1,FileName.Length-FileName.LastIndexOf(".")-1);
  97. for (int i=0; i<AcceptedFileTypes.Length; i++) {
  98. if (ext == AcceptedFileTypes[i]) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. private string[] ReturnFilesArray() {
  105. if (CurrentImagesFolder.Value != "") {
  106. try {
  107. string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
  108. string ImageFolderPath = AppPath + CurrentImagesFolder.Value;
  109. string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*");
  110. return FilesArray;
  111. } catch {
  112. return null;
  113. }
  114. } else {
  115. return null;
  116. }
  117. }
  118. private string[] ReturnDirectoriesArray() {
  119. if (CurrentImagesFolder.Value != "") {
  120. try {
  121. string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
  122. string CurrentFolderPath = AppPath + CurrentImagesFolder.Value;
  123. string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*");
  124. return DirectoriesArray ;
  125. } catch {
  126. return null;
  127. }
  128. } else {
  129. return null;
  130. }
  131. }
  132. public void DisplayImages() {
  133. string[] FilesArray = ReturnFilesArray();
  134. string[] DirectoriesArray = ReturnDirectoriesArray();
  135. string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
  136. string AppUrl;
  137. //Get the application's URL
  138. if (Request.ApplicationPath == "/")
  139. AppUrl = Request.ApplicationPath;
  140. else
  141. AppUrl = Request.ApplicationPath + "/";
  142. GalleryPanel.Controls.Clear();
  143. if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {
  144. gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.Value;
  145. } else {
  146. string ImageFileName = "";
  147. string ImageFileLocation = "";
  148. int thumbWidth = 94;
  149. int thumbHeight = 94;
  150. if (CurrentImagesFolder.Value != RootImagesFolder.Value) {
  151. System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
  152. myHtmlImage.Src = AppUrl + "images/ftb/folder.up.gif";
  153. myHtmlImage.Attributes["unselectable"]="on"; 
  154. myHtmlImage.Attributes["align"]="absmiddle"; 
  155. myHtmlImage.Attributes["vspace"]="36"; 
  156. string ParentFolder = CurrentImagesFolder.Value.Substring(0,CurrentImagesFolder.Value.LastIndexOf("\"));
  157. System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
  158. myImageHolder.CssClass = "imageholder";
  159. myImageHolder.Attributes["unselectable"]="on"; 
  160. myImageHolder.Attributes["onclick"]="divClick(this,'');";  
  161. myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + ParentFolder.Replace("\","\\") + "');";  
  162. myImageHolder.Controls.Add(myHtmlImage);
  163. System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
  164. myMainHolder.CssClass = "imagespacer";
  165. myMainHolder.Controls.Add(myImageHolder);
  166. System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
  167. myTitleHolder.CssClass = "titleHolder";
  168. myTitleHolder.Controls.Add(new LiteralControl("Up"));
  169. myMainHolder.Controls.Add(myTitleHolder);
  170. GalleryPanel.Controls.Add(myMainHolder);
  171. }
  172. foreach (string _Directory in DirectoriesArray) {
  173. try {
  174. string DirectoryName = _Directory.ToString();
  175. System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
  176. myHtmlImage.Src = AppUrl + "images/ftb/folder.big.gif";
  177. myHtmlImage.Attributes["unselectable"]="on"; 
  178. myHtmlImage.Attributes["align"]="absmiddle"; 
  179. myHtmlImage.Attributes["vspace"]="29"; 
  180. System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
  181. myImageHolder.CssClass = "imageholder";
  182. myImageHolder.Attributes["unselectable"]="on"; 
  183. myImageHolder.Attributes["onclick"]="divClick(this);";  
  184. myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.Value + "','" + DirectoryName.Replace(AppPath,"").Replace("\","\\") + "');";  
  185. myImageHolder.Controls.Add(myHtmlImage);
  186. System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
  187. myMainHolder.CssClass = "imagespacer";
  188. myMainHolder.Controls.Add(myImageHolder);
  189. System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
  190. myTitleHolder.CssClass = "titleHolder";
  191. myTitleHolder.Controls.Add(new LiteralControl(DirectoryName.Replace(AppPath + CurrentImagesFolder.Value + "\","")));
  192. myMainHolder.Controls.Add(myTitleHolder);
  193. GalleryPanel.Controls.Add(myMainHolder);
  194. } catch {
  195. // nothing for error
  196. }
  197. }
  198. foreach (string ImageFile in FilesArray) {
  199. try {
  200. ImageFileName = ImageFile.ToString();
  201. ImageFileName = ImageFileName.Substring(ImageFileName.LastIndexOf("\")+1);
  202. ImageFileLocation = AppUrl;
  203. ImageFileLocation = ImageFileLocation.Substring(ImageFileLocation.LastIndexOf("\")+1);
  204. //galleryfilelocation += "/";
  205. ImageFileLocation += CurrentImagesFolder.Value;
  206. ImageFileLocation += "/";
  207. ImageFileLocation += ImageFileName;
  208. System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
  209. myHtmlImage.Src = ImageFileLocation;
  210. System.Drawing.Image myImage = System.Drawing.Image.FromFile(ImageFile.ToString());
  211. myHtmlImage.Attributes["unselectable"]="on";  
  212. //myHtmlImage.border=0;
  213. // landscape image
  214. if (myImage.Width > myImage.Height) {
  215. if (myImage.Width > thumbWidth) {
  216. myHtmlImage.Width = thumbWidth;
  217. myHtmlImage.Height = Convert.ToInt32(myImage.Height * thumbWidth/myImage.Width);
  218. } else {
  219. myHtmlImage.Width = myImage.Width;
  220. myHtmlImage.Height = myImage.Height;
  221. }
  222. // portrait image
  223. } else {
  224. if (myImage.Height > thumbHeight) {
  225. myHtmlImage.Height = thumbHeight;
  226. myHtmlImage.Width = Convert.ToInt32(myImage.Width * thumbHeight/myImage.Height);
  227. } else {
  228. myHtmlImage.Width = myImage.Width;
  229. myHtmlImage.Height = myImage.Height;
  230. }
  231. }
  232. if (myHtmlImage.Height < thumbHeight) {
  233. myHtmlImage.Attributes["vspace"] = Convert.ToInt32((thumbHeight/2)-(myHtmlImage.Height/2)).ToString(); 
  234. }
  235. System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();
  236. myImageHolder.CssClass = "imageholder";
  237. myImageHolder.Attributes["onclick"]="divClick(this,'" + ImageFileName + "');";  
  238. myImageHolder.Attributes["ondblclick"]="returnImage('" + ImageFileLocation.Replace("\","/") + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";  
  239. myImageHolder.Controls.Add(myHtmlImage);
  240. System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
  241. myMainHolder.CssClass = "imagespacer";
  242. myMainHolder.Controls.Add(myImageHolder);
  243. System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
  244. myTitleHolder.CssClass = "titleHolder";
  245. myTitleHolder.Controls.Add(new LiteralControl(ImageFileName + "<BR>" + myImage.Width.ToString() + "x" + myImage.Height.ToString()));
  246. myMainHolder.Controls.Add(myTitleHolder);
  247. //GalleryPanel.Controls.Add(myImage);
  248. GalleryPanel.Controls.Add(myMainHolder);
  249. myImage.Dispose();
  250. } catch {
  251. }
  252. }
  253. gallerymessage.Text = "";
  254. }
  255. }
  256. </script>
  257. <asp:panel id="MainPage" runat="server" visible="false">
  258. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" >
  259. <HTML>
  260. <HEAD>
  261. <META HTTP-EQUIV="Expires" CONTENT="0">
  262. <title>安泰自动办公系统</title>
  263. <style>
  264. body {
  265. margin: 0px 0px 0px 0px;
  266. padding: 0px 0px 0px 0px;
  267. background: #ffffff; 
  268. width: 100%;
  269. overflow:hidden;
  270. border: 0;
  271. }
  272. body,tr,td {
  273. color: #000000;
  274. font-family: Verdana, Arial, Helvetica, sans-serif;
  275. font-size: 10pt;
  276. }
  277. div.imagespacer {
  278. width: 120;
  279. height: 126;
  280. text-align: center;
  281. float: left;
  282. font: 10pt verdana;
  283. margin: 5px;
  284. overflow: hidden;
  285. }
  286. div.imageholder {
  287. margin: 0px;
  288. padding: 0px;
  289. border: 1 solid #CCCCCC;
  290. width: 100;
  291. height: 100;
  292. }
  293. div.titleholder {
  294. font-family: ms sans serif, arial;
  295. font-size: 8pt;
  296. width: 100;
  297. text-overflow: ellipsis;
  298. overflow: hidden;
  299. white-space: nowrap;
  300. }
  301. </style>
  302. <script language="javascript">
  303. lastDiv = null;
  304. function divClick(theDiv,filename) {
  305. if (lastDiv) {
  306. lastDiv.style.border = "1 solid #CCCCCC";
  307. }
  308. lastDiv = theDiv;
  309. theDiv.style.border = "2 solid #316AC5";
  310. document.getElementById("FileToDelete").value = filename;
  311. }
  312. function gotoFolder(rootfolder,newfolder) {
  313. window.navigate("ftb.imagegallery.aspx?frame=1&rif=" + rootfolder + "&cif=" + newfolder);
  314. }
  315. function returnImage(imagename,width,height) {
  316. var arr = new Array();
  317. arr["filename"] = imagename;  
  318. arr["width"] = width;  
  319. arr["height"] = height;  
  320. window.parent.returnValue = arr;
  321. window.parent.close();
  322. }
  323. </script>
  324. </HEAD>
  325. <body>
  326. <table width=100% height=100% cellpadding=0 cellspacing=0 border=0>
  327. <FORM encType="multipart/form-data" runat="server">
  328. <tr><td>
  329. <div id="galleryarea" style="width=100%; height:100%; overflow: auto;">
  330. <asp:label id="gallerymessage" runat="server"></asp:label>
  331. <asp:panel id="GalleryPanel" runat="server"></asp:panel>
  332. </div>
  333. </td></tr>
  334. <asp:Panel id="UploadPanel" runat="server">
  335. <tr><td height=16 style="padding-left:10px;border-top: 1 solid #999999; background-color:#99ccff;">
  336. <table>
  337. <tr>
  338. <td valign=top><input id="UploadFile" type="file" name="UploadFile" runat="server" style="width:300;"/></td>
  339. <td valign=top><asp:button id="UploadImage" Text="上传" runat="server" onclick="UploadImage_OnClick" /></td>
  340. <td valign=top><asp:button id="DeleteImage" Text="删除" runat="server" onclick="DeleteImage_OnClick" /></td>
  341. <td valign=middle>
  342. </tr>
  343. <tr>
  344. <td colspan=3>
  345. <asp:RegularExpressionValidator runat="server" 
  346. ControlToValidate="UploadFile" 
  347. id="FileValidator" display="dynamic"/>
  348. <asp:literal id="ResultsMessage" runat="server" />
  349. </td>
  350. </tr></table>
  351. <input type="hidden" id="FileToDelete" Value="" runat="server" />
  352. <input type="hidden" id="RootImagesFolder" Value="images" runat="server" />
  353. <input type="hidden" id="CurrentImagesFolder" Value="images" runat="server" />
  354. </td></tr>
  355. </asp:panel>
  356. </form>
  357. </table>
  358. </body>
  359. </HTML>
  360. </asp:panel>
  361. <asp:panel id="iframePanel" runat="server" >
  362. <html> 
  363. <head><title>Insert Image</title></head>
  364. <style>
  365. body {
  366. margin: 0px 0px 0px 0px;
  367. padding: 0px 0px 0px 0px;
  368. background: #ffffff;
  369. overflow:hidden;
  370. }
  371. </style>
  372. <body>
  373. <iframe style="width:100%;height:100%;border:0;" border=0 frameborder=0 src="ftb.imagegallery.aspx?frame=1&<%=Request.QueryString%>"></iframe>
  374. </body>
  375. </html>
  376. </asp:panel>