ColumnEdit.aspx.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using CB.Common;
  8. using CB.Data;
  9. using CB.Entity;
  10. using CB.Framework;
  11. using System.IO;
  12. namespace CB.Admin.Plugins.SpecialColumn
  13. {
  14. public partial class ColumnEdit : AdminPage
  15. {
  16. string imgpath = "";
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (!Page.IsPostBack)
  20. {
  21. InitData();
  22. }
  23. }
  24. protected override void InitData()
  25. {
  26. var entity = CB.Data.ColumnService.Get(WRequest.GetQueryInt("id"));
  27. if (null != entity)
  28. {
  29. txtName.Text = entity.Name;
  30. txtRewriteUrl.Text = entity.RewriteUrl;
  31. imglogo.ImageUrl = entity.Logo;
  32. ddlLottery.SelectedValue = entity.Lottery;
  33. ddlType.SelectedValue = entity.TypeName;
  34. txtStatus.Text = entity.Status.ToString();
  35. txtWords.Text = entity.Words;
  36. txtAbout.Text = entity.About;
  37. txtTitle.Text = entity.hTitle;
  38. txtKeywords.Text = entity.hKeywords;
  39. txtDescription.Text = entity.hDescription;
  40. labID.Text = entity.Id.ToString();
  41. }
  42. base.InitData();
  43. }
  44. protected void btnUpdate_Click(object sender, EventArgs e)
  45. {
  46. var entity = new ColumnInfo()
  47. {
  48. Id = TypeConverter.StrToInt(labID.Text),
  49. Name = txtName.Text.Trim(),
  50. RewriteUrl = txtRewriteUrl.Text.Trim(),
  51. Logo =imglogo.ImageUrl,
  52. Lottery = ddlLottery.SelectedValue.Trim(),
  53. TypeName = ddlType.SelectedValue.Trim(),
  54. Status = TypeConverter.StrToInt(txtStatus.Text),
  55. Words = txtWords.Text.Trim(),
  56. About = txtAbout.Text.Trim(),
  57. hTitle = txtTitle.Text.Trim(),
  58. hKeywords = txtKeywords.Text.Trim(),
  59. hDescription = txtDescription.Text.Trim()
  60. };
  61. if (string.IsNullOrEmpty(entity.Name))
  62. {
  63. ShowMessageBox("名称不能为空!"); return;
  64. }
  65. if (string.IsNullOrEmpty(entity.RewriteUrl))
  66. {
  67. ShowMessageBox("URL重写不能为空!"); return;
  68. }
  69. if (string.IsNullOrEmpty(entity.Lottery))
  70. {
  71. ShowMessageBox("彩种不能为空!"); return;
  72. }
  73. if (string.IsNullOrEmpty(entity.TypeName))
  74. {
  75. ShowMessageBox("类型不能为空!"); return;
  76. }
  77. bool ok = false;
  78. ok = 0 < entity.Id ? CB.Data.ColumnService.Update(entity) : CB.Data.ColumnService.Save(entity);
  79. if (ok)
  80. { ShowMessageBox("保存成功!", "ColumnEdit.aspx?authPage=" + authPage + "&id=" + labID.Text); }
  81. else
  82. { ShowMessageBox("保存失败!"); }
  83. }
  84. protected void btnUpload_Click(object sender, EventArgs e)
  85. {
  86. if (upfile.HasFile)
  87. {
  88. string fileExt = Path.GetExtension(upfile.FileName).ToString().ToLower();
  89. if (!CheckFileExt(fileExt, ".jpg,.gif,.jpeg"))
  90. {
  91. ShowMessageBox("只允许上传的.JPG.gif.jepg图片文件!");
  92. }
  93. else
  94. {
  95. HttpPostedFile file = upfile.PostedFile;
  96. byte[] buffer = new byte[file.ContentLength];
  97. double lentk = (((double)file.ContentLength / (double)1024) / (double)1024);
  98. if (lentk < 1)
  99. {
  100. Stream inputStream = upfile.FileContent;
  101. inputStream.Read(buffer, 0, file.ContentLength);
  102. inputStream.Close();
  103. if (UploadFile(buffer,fileExt))
  104. {
  105. this.imglogo.ImageUrl = imgpath;
  106. lblmes.Text = "上传成功";
  107. }
  108. else
  109. {
  110. lblmes.Text = "上传失败";
  111. }
  112. }
  113. else
  114. {
  115. ShowMessageBox("您上传的文件过大,请重新上传!");
  116. }
  117. }
  118. }
  119. else
  120. {
  121. ShowMessageBox("未选择图库或未选择上传图片文件");
  122. }
  123. }
  124. #region 检查文件类型
  125. /// <summary>
  126. /// 检查文件类型
  127. /// </summary>
  128. /// <param name="fileExt"></param>
  129. /// <param name="fileExts"></param>
  130. /// <returns></returns>
  131. private bool CheckFileExt(string fileExt, string fileExts)
  132. {
  133. fileExt = fileExt.Substring(1);
  134. foreach (string str in fileExts.Split(new char[] { ',' }))
  135. {
  136. if (str.Trim().IndexOf(fileExt.Trim()) >= 0)
  137. {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. #endregion
  144. public bool UploadFile(byte[] fs, string fileExt)
  145. {
  146. bool ok = false;
  147. MemoryStream memory = null;
  148. FileStream file = null;
  149. string str = string.Empty;
  150. try
  151. {
  152. string path = Server.MapPath("/Static/images/Column/");
  153. if (!Directory.Exists(path))
  154. Directory.CreateDirectory(path);
  155. var entity = CB.Data.ColumnService.Get(WRequest.GetQueryInt("id"));
  156. string filename = "";
  157. string mapth = "";
  158. if (entity != null)
  159. {
  160. string logo =Server.MapPath(entity.Logo);
  161. if (!File.Exists(logo))
  162. {
  163. filename = Guid.NewGuid().ToString();
  164. }
  165. else
  166. {
  167. mapth = logo;
  168. }
  169. }
  170. else
  171. {
  172. filename = Guid.NewGuid().ToString();
  173. }
  174. memory = new MemoryStream(fs);
  175. if (mapth != "")
  176. {
  177. file = new FileStream(mapth, FileMode.Create);
  178. imgpath = entity.Logo;
  179. }
  180. else
  181. {
  182. file = new FileStream(string.Format("{0}{1}{2}", path, filename, fileExt), FileMode.Create);
  183. imgpath = string.Format("{0}{1}{2}", "/Static/images/Column/", filename, fileExt);
  184. }
  185. memory.WriteTo(file);
  186. ok = true;
  187. memory.Close();
  188. file.Close();
  189. }
  190. catch { }
  191. finally
  192. {
  193. if (memory != null)
  194. memory.Close();
  195. if (file != null)
  196. file.Close();
  197. }
  198. return ok;
  199. }
  200. }
  201. }