public async Task <IActionResult> PutSpecialty(int id, SpecialtyPModel specialtyPutModel) { var specialty = _context.Specialties.Find(id); if (specialty == null) { return(NotFound()); } //create image string newname = ""; if (specialtyPutModel.ImageBase64 != "") { string rootpath = _webHostEnvironment.WebRootPath; var nameDelete = specialty.Image .Substring(specialty.Image.LastIndexOf("/") + 1); try { System.IO.File.Delete(rootpath + "\\Images\\" + nameDelete); } catch (Exception) { } newname = specialtyPutModel.ImageName + "_" + id; using (FileStream fs = System.IO.File.Create(rootpath + "\\Assets\\" + newname)) { fs.Close(); System.IO.File.WriteAllBytes(rootpath + "\\Images" + newname, Convert.FromBase64String(specialtyPutModel.ImageBase64)); } } specialty.Name = specialtyPutModel.Name; specialty.Image = "freelancervn.somee.com/api/images/assets/" + newname; _context.Entry(specialtyPutModel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpecialtyExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }
public async Task <ActionResult <Specialty> > PostSpecialty(SpecialtyPModel specialtyPostModel) { var specialty = new Specialty() { Name = specialtyPostModel.Name }; _context.Specialties.Add(specialty); await _context.SaveChangesAsync(); string newname = specialtyPostModel.Name + "_" + specialty.Id; using (FileStream fs = System.IO.File.Create(rootpath + newname)) { fs.Close(); System.IO.File.WriteAllBytes(rootpath + "\\Images\\" + newname, Convert.FromBase64String(specialtyPostModel.ImageBase64)); } specialty.Name = specialtyPostModel.Name; specialty.Image = "freelancervn.somee.com/api/images/assets/" + newname; await _context.SaveChangesAsync(); return(specialty); }