public async Task <IActionResult> Create(TbGioiThieu tbGioiThieu, IFormFile GioithieuHinhanh) { if (ModelState.IsValid) { if (GioithieuHinhanh.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(GioithieuHinhanh.ContentDisposition).FileName.Trim('"'); string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(GioithieuHinhanh.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot\\Uploadimages\\{ImageName}"); var pathView = $"\\Uploadimages\\{ImageName}"; using (var stream = new FileStream(path, FileMode.Create)) { await GioithieuHinhanh.CopyToAsync(stream); } tbGioiThieu.GioithieuHinhanh = pathView; tbGioiThieu.GioithieuNgaytao = DateTime.Now; } _context.Add(tbGioiThieu); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(tbGioiThieu)); }
public async Task <IActionResult> Edit(int id, TbGioiThieu tbGioiThieu, IFormFile GioithieuHinhanh) { if (id != tbGioiThieu.GioithieuId) { return(NotFound()); } if (ModelState.IsValid) { if (GioithieuHinhanh != null) { var fileName = ContentDispositionHeaderValue.Parse(GioithieuHinhanh.ContentDisposition).FileName.Trim('"'); string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(GioithieuHinhanh.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot\\Uploadimages\\{ImageName}"); var pathView = $"\\Uploadimages\\{ImageName}"; using (var stream = new FileStream(path, FileMode.Create)) { await GioithieuHinhanh.CopyToAsync(stream); } tbGioiThieu.GioithieuHinhanh = pathView; } try { _context.Update(tbGioiThieu); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TbGioiThieuExists(tbGioiThieu.GioithieuId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(tbGioiThieu)); }