public IActionResult Post([FromForm] ElementDTO el) //public IActionResult Post([FromForm] IFormCollection el) { tblelement element = new tblelement(); try { // 上传图片到七牛云 string fileName = Guid.NewGuid().ToString() + ".png"; dynamic type = (new Program()).GetType(); string imgTempPath = Path.GetDirectoryName(type.Assembly.Location) + "\\" + fileName; //var photoBytes = Convert.FromBase64String(image); System.IO.File.WriteAllBytes(imgTempPath, el.file); //Bitmap imgFile = Base64StringToImage(el.file); //imgFile.Save(imgTempPath,ImageFormat.Png); //var stream = new FileStream(imgTempPath, FileMode.CreateNew); //el.Files[0].CopyTo(stream); //stream.Dispose(); HttpResult res = UploadFile(imgTempPath, fileName); if (res.Code != 200) { return(Ok(new ApiResultMutilObject <tblelement>() { code = EnumError.NET_FRE_UPLOADQINIU_ERROR, message = "NET_FRE_UPLOADQINIU_ERROR" })); } // 保存记录 element.mtitle = el.title; element.mdesc = el.desc; element.mhtml = el.html; element.mcss = el.css; element.mtype = el.type; element.userid = el.userid; element.mimg = fileName; MElementRepository.AddElement(element); System.IO.File.Delete(imgTempPath); } catch (Exception ex) { return(Ok(new ApiResultSingleObject <tblelement>() { code = EnumError.NET_FRE_UNKNOWN_ERROR, message = "NET_FRE_UNKNOWN_ERROR", })); } return(Ok(new ApiResultSingleObject <tblelement>() { code = EnumError.SUCCESS, message = "SUCCESS", data = element })); }
public static void AddElement(tblelement el) { try { using (var context = new CoreDbContext()) { // context.Add<tblelement>(el); context.tblelement.Add(el); context.SaveChanges(); } } catch (Exception ex) { // } }