Example #1
0
        public async Task <IActionResult> Adiciona(IFormFile file, Documento documento)
        {
            DocumentoDAO dao = new DocumentoDAO();

            if (file == null || file.Length == 0)
            {
                return(Content("file not selected"));
            }

            Documento temp = dao.DocumentoById(documento.Id);

            if (temp != null)
            {
                ViewBag.Documento = temp;
                return(View("Index"));
            }

            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", file.FileName);

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            if (file != null)
            {
                MemoryStream ms = new MemoryStream();
                file.OpenReadStream().CopyTo(ms);

                Documento tempDoc = new Documento
                {
                    Id        = documento.Id,
                    Titulo    = documento.Titulo,
                    Processo  = documento.Processo,
                    Categoria = documento.Categoria,
                    Anexo     = ms.ToArray(),
                    Nome      = file.FileName
                };

                dao.Insert(tempDoc);
            }
            ViewBag.Sucesso = true;
            return(View("Index"));
        }