public async Task <IActionResult> Create([Bind("id_HangBay,TenHangBay,MaHangBay,Logo")] HangBay hangBay, IFormFile image) { string fileName = string.Empty; if (image != null) { fileName = image.FileName; var path = Path.Combine(_hostingEnvironment.WebRootPath, "image\\logo", image.FileName.Trim()); if (System.IO.File.Exists(path)) { fileName = Guid.NewGuid().ToString() + image.FileName; path = Path.Combine(_hostingEnvironment.WebRootPath, "image\\logo", fileName); } var stream = new FileStream(path, FileMode.Create); await image.CopyToAsync(stream); stream.Close(); stream.Dispose(); } hangBay.Logo = fileName; _context.Add(hangBay); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(int id, [Bind("id_HangBay,TenHangBay,MaHangBay,Logo")] HangBay hangBay, IFormFile image) { try { var fileName = string.Empty; if (image == null) { _context.Update(hangBay); await _context.SaveChangesAsync(); } if (image != null) { if (hangBay.Logo == null) { fileName = image.FileName; if (System.IO.File.Exists(Path.Combine(_hostingEnvironment.WebRootPath, "image\\logo", fileName))) { fileName = Guid.NewGuid().ToString() + image.FileName; } hangBay.Logo = fileName; } else { fileName = hangBay.Logo; } var path = Path.Combine(_hostingEnvironment.WebRootPath, "image\\logo", fileName); var stream = new FileStream(path, FileMode.Create); await image.CopyToAsync(stream); stream.Close(); stream.Dispose(); _context.Update(hangBay); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!HangBayExists(hangBay.id_HangBay)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); }