public async Task <IActionResult> Edit(int id, [Bind("IdAnimal,Especie,Sexo,Nome,Foto,IdadeAprox,Porte,Cor,Raca,Historia,StatusAnimal,Adotavel")] Animaisccz animaisccz, IFormFile NovaFoto) { if (id != animaisccz.IdAnimal) { return(NotFound()); } if (ModelState.IsValid) { try { if (NovaFoto != null) { string pasta = Path.Combine(webHostEnvironment.WebRootPath, "img\\animaisccz"); 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); }; animaisccz.Foto = "/img/animaisccz/" + nomeArquivo; } _context.Update(animaisccz); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AnimaiscczExists(animaisccz.IdAnimal)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CaminhoFoto"] = webHostEnvironment.WebRootPath; return(View(animaisccz)); }
public async Task <IActionResult> Create([Bind("IdAnimal,Especie,Sexo,Nome,Foto,IdadeAprox,Porte,Cor,Raca,Historia,StatusAnimal,Adotavel")] Animaisccz animaisccz, IFormFile Foto) { if (ModelState.IsValid) { if (Foto != null) { string pasta = Path.Combine(webHostEnvironment.WebRootPath, "img\\animaisccz"); 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); }; animaisccz.Foto = "/img/animaisccz/" + nomeArquivo; } _context.Add(animaisccz); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(animaisccz)); }