public ActionResult Add(NewsAddModel model) { if (!ModelState.IsValid) { return(Json(MVCHelper.GetJsonValidMsg(ModelState))); } string[] strs = model.ImgURL.Split(','); string[] formats = strs[0].Replace(";base64", "").Split(':'); string img = strs[1]; string format = formats[1]; string[] imgFormats = { "image/png", "image/jpg", "image/jpeg", "image/bmp", "IMAGE/PNG", "IMAGE/JPG", "IMAGE/JPEG", "IMAGE/BMP" }; byte[] imgBytes; if (!imgFormats.Contains(format)) { return(Json(new AjaxResult { Status = "0", Msg = "请选择正确的图片格式,支持png、jpg、jpeg、png格式" })); } string ext = "." + format.Split('/')[1]; try { imgBytes = Convert.FromBase64String(img); } catch (Exception ex) { return(Json(new AjaxResult { Status = "0", Msg = "图片解密错误" })); } try { long id = newService.AddNew(AdminId, model.Title, SaveImg(imgBytes, ext), model.Contents); //PageToStatic(id); } catch (DbEntityValidationException ex) { return(Json(new AjaxResult { Status = "0", Msg = ex.Message })); } return(Json(new AjaxResult { Status = "1", Data = "/admin/news/list" })); }
public IActionResult Add(NewsAddModel newsadd) { if (ModelState.IsValid) { var news = new News { AddTime = DateTime.Now, Title = newsadd.Title, Content = newsadd.Content }; myDBContent.Add(news); int v = myDBContent.SaveChanges(); if (v > 0) { return(Redirect("/Manage/News/Index")); } else { ModelState.AddModelError("Title", "保存失败!"); } } return(View()); }