// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Course).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(Course.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; // get, post, put, etc.... if (User.ID != 0) { // existing user var objFromDb = await _context.User.FindAsync(User.ID); if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"img\profile"); var extension = Path.GetExtension(files[0].FileName); var imagePath = Path.Combine(webRootPath, objFromDb.ProfilePic.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } using (var fileStream = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } User.ProfilePic = @"\img\profile\" + fileName + extension; } else { User.ProfilePic = objFromDb.ProfilePic; // no image uploaded, just readding it from db so we don't lose it } } _context.Attach(User).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(User.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }