public IHttpActionResult themHinh([FromBody] hinhAnh hinh) { try { if (db.SanPhams.FirstOrDefault(e => e.id_san_pham == hinh.idSanpham) == null) { return(StatusCode(HttpStatusCode.NotFound)); } for (int i = 0; i < hinh.base64.Count; i++) { HinhSP hinhTam = new HinhSP(); hinhTam.id_sp = hinh.idSanpham; byte[] imageBytes = Convert.FromBase64String(hinh.base64[i]); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); string fileName = hinh.idSanpham + "stt" + i + ".png"; image.Save(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/hinh/SanPham"), fileName)); hinhTam.url_hinh = "~/hinh/SanPham/" + fileName; db.HinhSPs.InsertOnSubmit(hinhTam); db.SubmitChanges(); } return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult xoa(int idHinh) { try { HinhSP hinh = db.HinhSPs.FirstOrDefault(e => e.id_hinh == idHinh); if (hinh == null) { return(StatusCode(HttpStatusCode.NotFound)); } db.HinhSPs.DeleteOnSubmit(hinh); db.SubmitChanges(); return(Ok()); }catch (Exception ex) { return(BadRequest(ex.Message)); } }