public async Task <IActionResult> Create([Bind("IdPostBlog,DataPublicacao,Autor,Conteudo,Foto,DescricaoImagem,Titulo,OlhoPost")] Postsblog postsblog, IFormFile Foto) { if (Foto != null) { string pasta = Path.Combine(webHostEnvironment.WebRootPath, "img\\postsblog"); var nomeArquivo = Guid.NewGuid().ToString() + " " + Foto.FileName; string caminhoArquivo = Path.Combine(pasta, nomeArquivo); using (var stream = new FileStream(caminhoArquivo, FileMode.Create)) { await Foto.CopyToAsync(stream); }; postsblog.Foto = "/img/postsblog/" + nomeArquivo; } _context.Add(postsblog); await _context.SaveChangesAsync(); return(View(postsblog)); }
public async Task <IActionResult> Edit(int id, [Bind("IdPostBlog,DataPublicacao,Autor,Conteudo,Foto,DescricaoImagem,Titulo,OlhoPost")] Postsblog postsblog, IFormFile NovaFoto) { if (id != postsblog.IdPostBlog) { return(NotFound()); } if (ModelState.IsValid) { try { if (NovaFoto != null) { string pasta = Path.Combine(webHostEnvironment.WebRootPath, "img\\postsblog"); var nomeArquivo = Guid.NewGuid().ToString() + " " + NovaFoto.FileName; string caminhoArquivo = Path.Combine(pasta, nomeArquivo); using (var stream = new FileStream(caminhoArquivo, FileMode.Create)) { await NovaFoto.CopyToAsync(stream); }; postsblog.Foto = "/img/postsblog/" + nomeArquivo; } _context.Update(postsblog); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostsblogExists(postsblog.IdPostBlog)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(postsblog)); }