Загрузите изображение в указанную папку и сохраните путь в базе данных .NET core
[HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind]Employee employee) { if (ModelState.IsValid) { var files = HttpContext.Request.Form.Files; foreach (var Image in files) { if (Image != null && Image.Length > 0) { var file = Image; var uploads = Path.Combine(_appEnvironment.WebRootPath, "uploads\\img"); if (file.Length > 0) { var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); employee.ImageName = fileName; } } } } db.Employees.Add(employee); db.SaveChanges(); return RedirectToAction("Edit", new { id = employee.Id,name=employee.FirstName}); } else { var errors = ModelState.Values.SelectMany(v => v.Errors); } return View(employee); }
Что я уже пробовал:
when i save image, image save successfully in database, but it takes full image path like this C:\Users\VIZO\Desktop\employee.jpg i dont want like this, i need to save image path somehting like this ~images\employee.jpg and in specific folder and same path should save in database, also if someone show me after saving correct path how i can view that image.