public async Task <IActionResult> Create(BookADS bookADS) { var image = bookADS.ImageFile; if (image != null) { string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(image.FileName); //Get url To Save string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", ImageName); using (var stream = new FileStream(SavePath, FileMode.Create)) { image.CopyTo(stream); } bookADS.ImagePath = ImageName; } bookADS.ImageFile = null; if (ModelState.IsValid) { _context.Add(bookADS); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(bookADS)); }
public async Task <IActionResult> PutBookADS(int id, BookADS bookADS) { if (id != bookADS.ID) { return(BadRequest()); } _context.Entry(bookADS).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookADSExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Edit(int id, [Bind("ID,Title,Description,Author,ISBN,College,Rate,ImagePath")] BookADS bookADS) { if (id != bookADS.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(bookADS); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookADSExists(bookADS.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(bookADS)); }
public async Task <ActionResult <BookADS> > PostBookADS(BookADS bookADS) { var image = bookADS.ImagePath; if (image != null) { string imageName = Guid.NewGuid() + ".jpg"; string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", imageName); var imageString = Regex.Replace(image, @"^data:image\/[a-zA-Z]+;base64,", string.Empty); byte[] imageBytes = Convert.FromBase64String(imageString); System.IO.File.WriteAllBytes(SavePath, imageBytes); bookADS.ImagePath = imageName; } _context.bookADs.Add(bookADS); await _context.SaveChangesAsync(); return(CreatedAtAction("GetBookADS", new { id = bookADS.ID }, bookADS)); }