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.IO; public partial class SysAdmin_Up_info : System.Web.UI.Page { public static string imgs; public static DateTime date; public static string id; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["id"] != null) { id = Request.QueryString["id"].ToString(); date = DateTime.Now; show(id); } } } #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 (Fupl.HasFile) { FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG, FileExtension.BMP }; string doc = Server.MapPath("~/" + imgUrl); if (IsAllowedExtension(Fupl, fe) && (Fup.PostedFile.ContentLength / 1024) < 10240) //头字节过滤非法图片 上传大小限制 { string fileName = Path.GetFileName(Fupl.FileName); fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(Fupl.FileName); //以当前日期做为 上传图片名字, path.GetExtension获取文件扩展名 imgs = imgUrl + fileName;//获取图片相对路径 doc += fileName; ImageUtility.MakeThumbImage(Fupl.PostedFile.InputStream, doc, 720, 450, "HW"); //Fup.PostedFile.SaveAs(doc); Fupl.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"); // 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 public void show(string id) {// rbtLanguage.SelectedItem.Value if (rbtLanguage.Text == "0") { string sql = "select tid,title_cn,body_cn,source from newstab where id='" + id + "'"; DataTable dt = DBH.DBHelper.GetDataSet(sql); this.TxtTitle.Text = dt.Rows[0]["title_cn"].ToString(); this.txtSource.Text = dt.Rows[0]["source"].ToString(); this.txtBody.Text = dt.Rows[0]["body_cn"].ToString(); } else { string sql = "select tid,title_en,body_en,source from newstab where id='" + id + "'"; DataTable dt = DBH.DBHelper.GetDataSet(sql); this.TxtTitle.Text = dt.Rows[0]["title_en"].ToString(); this.txtSource.Text = dt.Rows[0]["source"].ToString(); this.txtBody.Text = dt.Rows[0]["body_en"].ToString(); } } //保存添加新闻内容 protected void ibtSumit_Click(object sender, ImageClickEventArgs e) { string sql; if (rbtLanguage.Text == "0") { sql = " update newstab set title_cn='" + TxtTitle.Text + "',body_cn='" + txtBody.Text + "',editdate='" + date + "',source='" + txtSource.Text + "',imgs='"+imgs+"'where id='" + id + "' "; } else { sql = "update newstab set title_en='" + TxtTitle.Text + "',body_en='" + txtBody.Text + "',editdate='" + date + "',source='" + txtSource.Text + "',imgs='"+imgs+"'where id='" + id + "' "; } int i = DBH.DBHelper.ExecuteNonQuery(sql); if (i > 0) { Methods1.Methods1.MsgBox("修改成功", "list.aspx"); } else { this.RegisterStartupScript("", ""); } } protected void rbtLanguage_SelectedIndexChanged(object sender, EventArgs e) { show(id); } protected void rbtLanguage_TextChanged(object sender, EventArgs e) { show(id); } }