Exemple #1
0
        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));
        }
Exemple #2
0
        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));
        }