using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; using System.IO; using System.Data.SqlClient; public partial class SysAdmin_CultureInfo : System.Web.UI.Page { public static string imgs;//获取图片地址 public string editor; public static DateTime dtime; public static string suoImgs;//获取图片缩略图路径 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindDDL(); } dtime = DateTime.Now; if (Session["admin"] != null) { editor = Session["admin"].ToString(); } else editor = "匿名"; } public void bindDDL() { string str = "select tid,cn_tname from culturetypetab where fid!='0'"; DataTable dts = DBH.DBHelper.GetDataSet(str); ddltype.DataSource = dts; ddltype.DataTextField = "cn_tname"; ddltype.DataValueField = "tid"; ddltype.DataBind(); } public void addmsg() { string tid = ddltype.SelectedItem.Value; string sql; if (suoImgs == null) { //EN_content.Value = EN_content.Value.Replace("'", "''"); //FCKEditor1.value.Replace("'", "''"); sql = "insert into cultureinfotab(tid,cn_title,en_title,cn_content,en_content,cn_source,cn_editor,en_editor,edate)values(@tid,@txtCname,@txtEname,@CN_content,@EN_content,@txtSource,@editor,@editor1,@dtime)"; SqlParameter[] par ={ new SqlParameter("@tid",tid), new SqlParameter("@txtCname",txtCname.Text), new SqlParameter("@txtEname",txtEname.Text), new SqlParameter("@CN_content",CN_content.Value), new SqlParameter("@EN_content",EN_content.Value.Replace("'","‘")), new SqlParameter("@txtSource",txtSource.Text), new SqlParameter("@editor",editor), new SqlParameter("@editor1",editor), new SqlParameter("@dtime",dtime), }; int i = DBH.DBHelper.ExecuteNonQuery(sql, par); if (i > 0) { //Methods1.Methods1.Alert(this, "信息添加成功"); Methods1.Methods1.MsgBox("信息添加成功", "Cultureinfo.aspx"); this.txtCname.Text = ""; this.txtEname.Text = ""; this.txtSource.Text = ""; this.EN_content.Value = ""; } else { Methods1.Methods1.Alert(this, "信息添加失败"); } } else { sql = @"insert into cultureinfotab(tid,cn_title,en_title,cn_content,en_content,cn_source,cn_editor,en_editor,Edate,imgs)values(@tid,@txtCname,@txtEname,@CN_content,@EN_content,@txtSource,@editor,@editor1,@dtime,@suoImgs)"; SqlParameter[] par ={ new SqlParameter("@tid",tid), new SqlParameter("@txtCname",txtCname.Text), new SqlParameter("@txtEname",txtEname.Text), new SqlParameter("@CN_content",CN_content.Value), new SqlParameter("@EN_content",EN_content.Value.Replace("'","‘")), new SqlParameter("@txtSource",txtSource.Text), new SqlParameter("@editor",editor), new SqlParameter("@editor1",editor), new SqlParameter("@dtime",dtime), new SqlParameter("@suoImgs",suoImgs) }; int i = DBH.DBHelper.ExecuteNonQuery(sql, par); if (i > 0) { //Methods1.Methods1.Alert(this, "信息添加成功"); Methods1.Methods1.MsgBox("信息添加成功", "Cultureinfo.aspx"); } else { Methods1.Methods1.Alert(this, "信息添加失败"); } } ////sql = sql.Replace("'", "/'"); //int i = DBH.DBHelper.ExecuteNonQuery(sql,par); //if (i > 0) //{ // Methods1.Methods1.Alert(this, "信息添加成功"); //} //else //{ // Methods1.Methods1.Alert(this, "信息添加失败"); //} } protected void ib1Save_Click(object sender, ImageClickEventArgs e) { addmsg(); } #region /// /// 上传图片 /// /// /// protected void imgbtnUp_Click(object sender, ImageClickEventArgs e) { string imgUrl = "Upload/" + DateTime.Now.ToString("yyyyMMdd") + "/";//以当前日期为 今天上传图片目录 if (!System.IO.Directory.Exists(Server.MapPath("~/" + imgUrl))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/" + imgUrl));//路径不存在 则创建该目录 } if (Fup.HasFile) { FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG, FileExtension.BMP }; string doc = Server.MapPath("~/" + imgUrl); if (IsAllowedExtension(Fup, fe) && (Fup.PostedFile.ContentLength / 1024) < 10240) //头字节过滤非法图片 上传大小限制 { string fileName = Path.GetFileName(Fup.FileName); fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(Fup.FileName); //以当前日期做为 上传图片名字, path.GetExtension获取文件扩展名 imgs = imgUrl + fileName;//获取图片相对路径 doc += fileName; ImageUtility.MakeThumbImage(Fup.PostedFile.InputStream, doc, 720, 450, "HW"); //Fup.PostedFile.SaveAs(doc); Fup.Dispose(); //Stream s= Fup.PostedFile.InputStream; //// p1.Visible = true; //生成缩略图 if (!System.IO.Directory.Exists(Server.MapPath("~/" + imgUrl + "/suo/"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/" + imgUrl + "/suo/"));//路径不存在 则创建该目录 } string simg = Server.MapPath("~/" + imgUrl + "suo/" + fileName); string jieguo = ImageUtility.MakeThumbImage(doc, simg, 220, 168, "HW"); suoImgs = simg;//获取上传图片缩略图的路径名称 // this.RegisterStartupScript("", ""); //// img1.Src = "~/" + imgUrl + "suo/" + fileName; //// txtImg.Value = imgUrl + fileName; //// txtPic.Value = imgUrl + "suo/" + fileName; this.RegisterStartupScript("", ""); } else this.RegisterStartupScript("", ""); } } public bool IsAllowedExtension(FileUpload fu, FileExtension[] fileEx) { int contentLength = fu.PostedFile.ContentLength; byte[] buffer = new byte[contentLength]; fu.PostedFile.InputStream.Read(buffer, 0, contentLength); MemoryStream input = new MemoryStream(buffer); BinaryReader reader = new BinaryReader(input); string s = ""; try { s = reader.ReadByte().ToString(); s = s + reader.ReadByte().ToString(); } catch { } reader.Close(); input.Close(); foreach (FileExtension extension in fileEx) { if (int.Parse(s) == Convert.ToInt32(extension)) { return true; } } return false; } public enum FileExtension { GIF = 7173, PNG = 13780, JPG = 255216, BMP = 6677, } #endregion }