Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to save image path in database and save image in specific folder of project.NET core

$
0
0
[HttpPost][ValidateAntiForgeryToken]publicasyncTask<IActionResult>Create(Employee emp){if(ModelState.IsValid){var files =HttpContext.Request.Form.Files;foreach(varImagein files){if(Image!=null&&Image.Length>0){var file =Image;//There is an error herevar uploads =Path.Combine(_appEnvironment.WebRootPath,"uploads\\img");if(file.Length>0){var fileName =Guid.NewGuid().ToString().Replace("-","")+Path.GetExtension(file.FileName);using(var fileStream =newFileStream(Path.Combine(uploads, fileName),FileMode.Create)){await file.CopyToAsync(fileStream);
                            emp.BookPic= fileName;}}}}
            _context.Add(emp);await _context.SaveChangesAsync();returnRedirectToAction("Index");}else{var errors =ModelState.Values.SelectMany(v => v.Errors);}returnView(emp);}

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 something like this ~images\employee.jpg. The other problem is image doesnt saving in specific folder of project, i need to save image in this path of my project 

uploads\\img


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>