using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CB.Common; using CB.Data; using CB.Entity; using CB.Framework; using System.IO; namespace CB.Admin.Plugins.SpecialColumn { public partial class ColumnEdit : AdminPage { string imgpath = ""; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { InitData(); } } protected override void InitData() { var entity = CB.Data.ColumnService.Get(WRequest.GetQueryInt("id")); if (null != entity) { txtName.Text = entity.Name; txtRewriteUrl.Text = entity.RewriteUrl; imglogo.ImageUrl = entity.Logo; ddlLottery.SelectedValue = entity.Lottery; ddlType.SelectedValue = entity.TypeName; txtStatus.Text = entity.Status.ToString(); txtWords.Text = entity.Words; txtAbout.Text = entity.About; txtTitle.Text = entity.hTitle; txtKeywords.Text = entity.hKeywords; txtDescription.Text = entity.hDescription; labID.Text = entity.Id.ToString(); } base.InitData(); } protected void btnUpdate_Click(object sender, EventArgs e) { var entity = new ColumnInfo() { Id = TypeConverter.StrToInt(labID.Text), Name = txtName.Text.Trim(), RewriteUrl = txtRewriteUrl.Text.Trim(), Logo =imglogo.ImageUrl, Lottery = ddlLottery.SelectedValue.Trim(), TypeName = ddlType.SelectedValue.Trim(), Status = TypeConverter.StrToInt(txtStatus.Text), Words = txtWords.Text.Trim(), About = txtAbout.Text.Trim(), hTitle = txtTitle.Text.Trim(), hKeywords = txtKeywords.Text.Trim(), hDescription = txtDescription.Text.Trim() }; if (string.IsNullOrEmpty(entity.Name)) { ShowMessageBox("名称不能为空!"); return; } if (string.IsNullOrEmpty(entity.RewriteUrl)) { ShowMessageBox("URL重写不能为空!"); return; } if (string.IsNullOrEmpty(entity.Lottery)) { ShowMessageBox("彩种不能为空!"); return; } if (string.IsNullOrEmpty(entity.TypeName)) { ShowMessageBox("类型不能为空!"); return; } bool ok = false; ok = 0 < entity.Id ? CB.Data.ColumnService.Update(entity) : CB.Data.ColumnService.Save(entity); if (ok) { ShowMessageBox("保存成功!", "ColumnEdit.aspx?authPage=" + authPage + "&id=" + labID.Text); } else { ShowMessageBox("保存失败!"); } } protected void btnUpload_Click(object sender, EventArgs e) { if (upfile.HasFile) { string fileExt = Path.GetExtension(upfile.FileName).ToString().ToLower(); if (!CheckFileExt(fileExt, ".jpg,.gif,.jpeg")) { ShowMessageBox("只允许上传的.JPG.gif.jepg图片文件!"); } else { HttpPostedFile file = upfile.PostedFile; byte[] buffer = new byte[file.ContentLength]; double lentk = (((double)file.ContentLength / (double)1024) / (double)1024); if (lentk < 1) { Stream inputStream = upfile.FileContent; inputStream.Read(buffer, 0, file.ContentLength); inputStream.Close(); if (UploadFile(buffer,fileExt)) { this.imglogo.ImageUrl = imgpath; lblmes.Text = "上传成功"; } else { lblmes.Text = "上传失败"; } } else { ShowMessageBox("您上传的文件过大,请重新上传!"); } } } else { ShowMessageBox("未选择图库或未选择上传图片文件"); } } #region 检查文件类型 /// /// 检查文件类型 /// /// /// /// private bool CheckFileExt(string fileExt, string fileExts) { fileExt = fileExt.Substring(1); foreach (string str in fileExts.Split(new char[] { ',' })) { if (str.Trim().IndexOf(fileExt.Trim()) >= 0) { return true; } } return false; } #endregion public bool UploadFile(byte[] fs, string fileExt) { bool ok = false; MemoryStream memory = null; FileStream file = null; string str = string.Empty; try { string path = Server.MapPath("/Static/images/Column/"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); var entity = CB.Data.ColumnService.Get(WRequest.GetQueryInt("id")); string filename = ""; string mapth = ""; if (entity != null) { string logo =Server.MapPath(entity.Logo); if (!File.Exists(logo)) { filename = Guid.NewGuid().ToString(); } else { mapth = logo; } } else { filename = Guid.NewGuid().ToString(); } memory = new MemoryStream(fs); if (mapth != "") { file = new FileStream(mapth, FileMode.Create); imgpath = entity.Logo; } else { file = new FileStream(string.Format("{0}{1}{2}", path, filename, fileExt), FileMode.Create); imgpath = string.Format("{0}{1}{2}", "/Static/images/Column/", filename, fileExt); } memory.WriteTo(file); ok = true; memory.Close(); file.Close(); } catch { } finally { if (memory != null) memory.Close(); if (file != null) file.Close(); } return ok; } } }