| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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 检查文件类型
- /// <summary>
- /// 检查文件类型
- /// </summary>
- /// <param name="fileExt"></param>
- /// <param name="fileExts"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|