/// <summary> /// 删除公告 /// </summary> /// <param name="onticeId"></param> /// <returns></returns> public ActionResult NoticeDelete(int NoticeID) { try { B_NoticeTable dbNoticeTable = (from tbNoticeTable in myDYXTEntities.B_NoticeTable where tbNoticeTable.NoticeID == NoticeID select tbNoticeTable).Single(); //删除数据 myDYXTEntities.B_NoticeTable.Remove(dbNoticeTable); //--->删除插图 公告内容 txt文件路径 string NoticeTablepath = Server.MapPath("~/Document/Notice/Text/") + dbNoticeTable.NoticeContent; if (System.IO.File.Exists(NoticeTablepath)) { //读取原始的文本内容 //存在文件 string oldTextContent = System.IO.File.ReadAllText(NoticeTablepath); //匹配出文件名称 删除 MatchCollection oldMatchs = Regex.Matches(oldTextContent, "(?<=Document/Notice/).+?(?=\".+?/>)"); foreach (Match match in oldMatchs) { // 删除文件 string dfilePath = Server.MapPath("~/Document/Notice/") + match.Value; System.IO.File.Delete(dfilePath); } } //--->删除公告内容txt文件 System.IO.File.Delete(NoticeTablepath); myDYXTEntities.SaveChanges(); return(Json(true, JsonRequestBehavior.AllowGet)); } catch (Exception e) { Console.WriteLine(e); //"数据异常!"; } return(Json(false, JsonRequestBehavior.AllowGet)); }
[ValidateInput(false)]//由于要存入HTML,关闭验证 public ActionResult NoticeUpdate(B_NoticeTable B_Notice, HttpPostedFileBase noticeCarouseImage) { try { int User = Convert.ToInt32(Session["AccountID"].ToString()); //保存的插入的图片 List <string> savedImageList = new List <string>(); //原始的插入图片 List <string> oldSavedImageList = new List <string>(); //检查公告标题不为空 if (!string.IsNullOrEmpty(B_Notice.Title)) { B_NoticeTable dbNotice = (from tbNoticeTable in myDYXTEntities.B_NoticeTable where tbNoticeTable.NoticeID == B_Notice.NoticeID select tbNoticeTable).Single(); //检查 存放公告内容的 目录是否存在,不存在就创建 if (!Directory.Exists(Server.MapPath("~/Document/Notice/Text/"))) { Directory.CreateDirectory(Server.MapPath("~/Document/Notice/Text/")); } //检查目录是否存在,不存在就创建 if (!Directory.Exists(Server.MapPath("~/Document/Notice/NoticeCarousel/"))) { Directory.CreateDirectory(Server.MapPath("~/Document/Notice/NoticeCarousel/")); } //--->用txt文件来保存公告内容 //txt文件名称 string fileName; //txt文件路径 string filePath; //加载公告内容 string oldFilePath = Server.MapPath("~/Document/Notice/Text/") + dbNotice.NoticeContent; if (System.IO.File.Exists(oldFilePath)) { //读取原始的文本内容 //存在文件 string oldTextContent = System.IO.File.ReadAllText(oldFilePath); //匹配出文件名称 保存到list oldSavedImageList MatchCollection oldMatchs = Regex.Matches(oldTextContent, "(?<=Document/Notice/).+?(?=\".+?/>)"); foreach (Match match in oldMatchs) { oldSavedImageList.Add(match.Value); } //获取文件名称 fileName = dbNotice.NoticeContent; //文件路径 filePath = oldFilePath; } else { //不存在文件 fileName = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd") + "-" + Guid.NewGuid() + ".txt"; //文件路径 filePath = Server.MapPath("~/Document/Notice/Text/") + fileName; } //txt文件内容 string textContent = B_Notice.NoticeContent; //-----获取最终保存名称到数据中的文件 MatchCollection matchs = Regex.Matches(textContent, "(?<=Document/Notice/).+?(?=\".+?/>)"); foreach (Match match in matchs) { savedImageList.Add(match.Value); } //检查没有使用的插图,将其删除 //遍历原来的插图文件列表oldSavedImageList,如在现在的文件列表savedImageList,则在使用; //如不在,则未使用应该移除 foreach (string str in oldSavedImageList) { //判断是否存在,不存在就删除 if (!savedImageList.Contains(str)) { //删除文件 string dfilePath = Server.MapPath("~/Document/Notice/") + str; try { System.IO.File.Delete(dfilePath); } catch (Exception e) { Console.WriteLine(e); } } } //获取上传的临时插图文件表(未保存的) List <string> tempFile = new List <string>(); if (Session["tempEditorFile"] != null) { tempFile = Session["tempEditorFile"] as List <string>; } if (tempFile != null) { string dFilePath = Server.MapPath("~/Document/Notice/"); //遍历临时文件表 foreach (string s in tempFile) { //当临时文件表中的文件 不存在 于被保存的文件时,删除该文件,避免文件冗余 if (!savedImageList.Contains(s)) { string strdeletePath = dFilePath + s; try { System.IO.File.Delete(strdeletePath); } catch (Exception e) { Console.WriteLine(e); } } } } //保存文件 公告内容的txt文件 TextWriter textWriter = new StreamWriter(filePath, false, new System.Text.UTF8Encoding(false)); textWriter.Write(textContent); textWriter.Close(); //更新公告信息 dbNotice.NoticeTypeID = B_Notice.NoticeTypeID; dbNotice.NoticeStatusID = B_Notice.NoticeStatusID; dbNotice.IssueTime = DateTime.Now;//发布时间 dbNotice.ArticleLabel = B_Notice.ArticleLabel; dbNotice.Title = B_Notice.Title; dbNotice.NoticeContent = fileName; //公告内容 dbNotice.AccountID = User; myDYXTEntities.Entry(dbNotice).State = System.Data.Entity.EntityState.Modified; //改为修改状态 myDYXTEntities.SaveChanges(); //移除session Session.Remove("tempEditorFile"); return(Json(true, JsonRequestBehavior.AllowGet)); } } catch (Exception e) { Console.WriteLine(e); } return(Json(false, JsonRequestBehavior.AllowGet)); }
[ValidateInput(false)]//由于要存入HTML,关闭验证 public ActionResult NoticeInsert(B_NoticeTable B_Notice, HttpPostedFileBase noticeCarouseImage) { try { int User = Convert.ToInt32(Session["AccountID"].ToString()); //定义list 存放需要保存的富文本框图片的名称 List <string> savedImageList = new List <string>(); if (!string.IsNullOrEmpty(B_Notice.Title)) { //检查 存放公告内容的 目录是否存在,不存在就创建 if (!Directory.Exists(Server.MapPath("~/Document/Notice/Text/"))) { Directory.CreateDirectory(Server.MapPath("~/Document/Notice/Text/")); } //--->用txt文件来保存公告内容 //txt文件名称 string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "-" + Guid.NewGuid() + ".txt"; //txt文件路径 string filePath = Server.MapPath("~/Document/Notice/Text/") + fileName; //txt文件内容 string textCount = B_Notice.NoticeContent; //-----获取最终保存的图片文件 MatchCollection matchs = System.Text.RegularExpressions.Regex.Matches(textCount, "(?<=/Document/Notice/).+?(?=\".+?/>)"); foreach (Match match in matchs) { savedImageList.Add(match.Value); } //保存文件 txt文件 //StreamWriter 文件路径,是否追加,文件编码 TextWriter textWriter = new StreamWriter(filePath, false, new System.Text.UTF8Encoding(false)); textWriter.Write(textCount); textWriter.Close(); //保存公告信息 B_Notice.AccountID = User; //登录用户 B_Notice.NoticeContent = fileName; //公告内容 B_Notice.IssueTime = DateTime.Now; //发布时间 //保存公告数据 myDYXTEntities.B_NoticeTable.Add(B_Notice); myDYXTEntities.SaveChanges(); //清理上传到富文本框中,后被删除(即未被使用的图片) //获取上传的临时图片文件表(未保存的) List <string> tempFile = new List <string>(); if (Session["tempEditorFile"] != null) { tempFile = Session["tempEditorFile"] as List <string>; } if (tempFile != null) { string dFilePath = Server.MapPath("~/Document/Notice/"); //遍历临时文件表 foreach (string img in tempFile) { //当临时文件表中的文件不存在于被保存的文件时,删除该文件,避免文件冗余 if (!savedImageList.Contains(img)) { string strdeletePath = dFilePath + img; try { System.IO.File.Delete(strdeletePath); } catch (Exception e) { } } } } return(Json(true, JsonRequestBehavior.AllowGet)); } } catch (Exception e) { } return(Json(false, JsonRequestBehavior.AllowGet)); }