예제 #1
0
 public ActionResult Edit(NewsEditModel model)
 {
     if (!ModelState.IsValid)
     {
         return(Json(MVCHelper.GetJsonValidMsg(ModelState)));
     }
     if (model.ImgURL.Contains(";base64"))
     {
         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
         {
             newService.Update(model.Id, model.Title, model.Contents, SaveImg(imgBytes, ext));
         }
         catch (DbEntityValidationException ex)
         {
             return(Json(new AjaxResult {
                 Status = "0", Msg = ex.Message
             }));
         }
     }
     else
     {
         newService.Update(model.Id, model.Title, model.Contents, model.ImgURL);
     }
     return(Json(new AjaxResult {
         Status = "1", Data = "/admin/news/list"
     }));
 }