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_NewsInfo : System.Web.UI.Page { public static string imgs; public static DateTime date; public static string tid; protected void Page_Load(object sender, EventArgs e) { //if (Request.QueryString["Tid"] != null) //{ tid = Methods1.Methods1.s(); date = DateTime.Now; //} } #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"); // 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 //保存添加新闻内容 protected void ibtSumit_Click(object sender, ImageClickEventArgs e) { string sql; sql = "insert into newstab(tid,title_cn,body_cn,imgs,editdate,source) values('" + tid + "','" + TxtTitle.Text.Trim() + "','" + CN_content.Value + "','" + imgs + "','" + date + "','" + txtSource.Text.Trim() + "') "; //} //else //{ // sql = "insert into newstab(tid,title_en,body_en,imgs,editdate,source) values('" + tid + "','" + TxtTitle.Text.Trim() + "','" + txtBody.Text.Trim() + "','" + imgs + "','" + date + "','" + txtSource.Text.Trim() + "') "; //} int i = DBH.DBHelper.ExecuteNonQuery(sql); if (i > 0) { Methods1.Methods1.MsgBox("添加成功", "list.aspx"); } else { this.RegisterStartupScript("", ""); } } }