public async Task <IActionResult> Update(int id, [Bind("Id,Title,Body,Footer,Url,Image,Quote")] CMSPage page, IFormFile image) { if (id != page.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (image != null && image.Length > 0) { var file = image; if (file.Length > 0) { var fileName = $"{CMSPage.MakeFriendly(Path.GetFileNameWithoutExtension(file.FileName))}{Path.GetExtension(file.FileName)}"; using (var fileStream = new FileStream(Path.Combine(uploadsPath, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } page.Image = fileName; } } var realPage = await _context.Pages.FindAsync(id); realPage.Title = page.Title; realPage.Footer = page.Footer; realPage.Body = page.Body; realPage.Url = page.Url; realPage.Quote = page.Quote; if (page.Image != null) { realPage.Image = page.Image; } _context.Update(realPage); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PageExists(page.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(page)); }
public async Task <IActionResult> ImageUpload(IFormFile image) { if (image != null && image.Length > 0) { var file = image; if (file.Length > 0) { var fileName = $"{CMSPage.MakeFriendly(Path.GetFileNameWithoutExtension(file.FileName))}.{Path.GetExtension(file.FileName)}"; using (var fileStream = new FileStream(Path.Combine(uploadsPath, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } } } return(View("Update")); }