public async Task <IActionResult> Create([Bind("Id,DateOfPost,Title,Blurb,Post,Image,FormFile")] Fitspo fitspo, IFormFile FormFile) { if (ModelState.IsValid) { byte[] image; using (var memoryStream = new MemoryStream()) { await FormFile.CopyToAsync(memoryStream); if (memoryStream.Length > 2097152) { ModelState.AddModelError("FormFile", "The file is too large."); } else { image = memoryStream.ToArray(); Fitspo fitspoNew = new Fitspo { Id = fitspo.Id, DateOfPost = fitspo.DateOfPost, Title = fitspo.Title, Blurb = fitspo.Blurb, Post = fitspo.Post, Image = image, FormFile = FormFile }; _context.Add(fitspoNew); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(fitspo)); } } return(View(fitspo)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,DateOfPost,Title,Blurb,Post,Image,FormFile")] Fitspo fitspo, IFormFile FormFile, string param) { if (id != fitspo.Id) { return(NotFound()); } byte[] image; if (ModelState.IsValid) { try { using (var memoryStream = new MemoryStream()) { await FormFile.CopyToAsync(memoryStream); if (memoryStream.Length > 2097152) { ModelState.AddModelError("FormFile", "The file is too large."); } else { image = memoryStream.ToArray(); Fitspo fitspoNew = new Fitspo { Id = fitspo.Id, DateOfPost = fitspo.DateOfPost, Title = fitspo.Title, Blurb = fitspo.Blurb, Post = fitspo.Post, Image = image, FormFile = FormFile }; _context.Update(fitspoNew); await _context.SaveChangesAsync(); } } } catch (DbUpdateConcurrencyException) { if (!FitspoExists(fitspo.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else if (fitspo.DateOfPost != null && fitspo.Title != null && fitspo.Blurb != null && fitspo.Post != null) { string imageBase64 = TempData["Image"].ToString(); byte[] imageByteArr = Convert.FromBase64String(imageBase64); Fitspo fitspoNew = new Fitspo { Id = fitspo.Id, DateOfPost = fitspo.DateOfPost, Title = fitspo.Title, Blurb = fitspo.Blurb, Post = fitspo.Post, Image = imageByteArr, FormFile = FormFile }; _context.Update(fitspoNew); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(fitspo)); }