public ActionResult Register(SellerCreatePath b) { if (ModelState.IsValid) { string uniqueFileName = null; // If the Photo property on the incoming model object is not null, then the user // has selected an image to upload. if (b.photo != null) { // The image must be uploaded to the images folder in wwwroot // To get the path of the wwwroot folder we are using the inject // HostingEnvironment service provided by ASP.NET Core string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); // To make sure the file name is unique we are appending a new // GUID value and and an underscore to the file name uniqueFileName = Guid.NewGuid().ToString() + "_" + b.photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder b.photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Seller newEmployee = new Seller { Sid = b.Sid, SName = b.Sname, Pass = b.Pass, Email = b.Email, Phno = b.Mobile, date = b.date, // Store the file name in PhotoPath property of the employee object // which gets saved to the Employees database table Photopath = uniqueFileName }; _context.Add(newEmployee); _context.SaveChanges(); return(RedirectToAction("Details", new { id = newEmployee.Sid })); } return(View()); //try //{ // _context.Add(b); // _context.SaveChanges(); // ViewBag.message = b.SName + " successfully registered"; // return RedirectToAction("Login"); //} //catch (Exception e) //{ // ViewBag.message = b.SName + " registration failed"; // return View(); //} }
public ActionResult RegisterSeller(SellerCreatePath bu) { string uniqueFileName = null; if (ModelState.IsValid) { // If the Photo property on the incoming model object is not null, then the user // has selected an image to upload. if (bu.photopath != null) { // The image must be uploaded to the images folder in wwwroot // To get the path of the wwwroot folder we are using the inject // HostingEnvironment service provided by ASP.NET Core string uploadsFolder = Path.Combine(hostingEnvir.WebRootPath, "images"); // To make sure the file name is unique we are appending a new // GUID value and and an underscore to the file name uniqueFileName = Guid.NewGuid().ToString() + "_" + bu.photopath.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder bu.photopath.CopyTo(new FileStream(filePath, FileMode.Create)); } } Seller newEmployee = new Seller { Sid = bu.Sid, Sname = bu.Sname, Pass = bu.Pass, Email = bu.Email, Phone = bu.Phone, Date = bu.Date, // Store the file name in PhotoPath property of the employee object // which gets saved to the Employees database table photopath = uniqueFileName }; Context.Add(newEmployee); Context.SaveChanges(); return(RedirectToAction("Details", new { id = newEmployee.Sid })); return(View()); }