Example #1
0
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            if (file?.ContentLength > 0)
            {
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);

                var route = Server.MapPath(serverFolderPath);

                route += "/" + pictureName;

                file.SaveAs(route);

                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Name = file.FileName;
                emoPicture.Path = $"{serverFolderPath}/{pictureName}";

                db.EmoPictures.Add(emoPicture);
                await db.SaveChangesAsync();

                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }
            return(View());
        }
        public async Task <ActionResult> IndexAsync(HttpPostedFileBase file)
        {
            if (file?.ContentLength > 0)
            {
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);

                var route = Server.MapPath(serverFolderPath);//optiene la ruta absoluta de la carpeta
                if (!Directory.Exists(route))
                {
                    Directory.CreateDirectory(route);
                }
                route = route + "/" + pictureName;


                file.SaveAs(@route);

                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Name = file.FileName;
                emoPicture.Path = $"{serverFolderPath}/{pictureName}";
                using (EmotionWebContext database = new EmotionWebContext())
                {
                    db.EmoPictures.Add(emoPicture);
                    await db.SaveChangesAsync();
                }


                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }
            return(View());
        }
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            // file?: pregunta si es nulo antes de realizar la validacion
            if (file?.ContentLength > 0)
            {
                // Generamos un nombre aleatorio para el archivo
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);

                // concatenamos la ruta con el nombre
                var route = Server.MapPath(serverFolderPath);
                route = route + "/" + pictureName;

                // lo guardamos en disco
                file.SaveAs(route);

                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Name = file.FileName;
                emoPicture.Path = $"{serverFolderPath}/{pictureName}";


                db.EmoPictures.Add(emoPicture);
                await db.SaveChangesAsync();

                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }
            return(View());
        }
        public async Task <ActionResult> IndexAsync(HttpPostedFileBase file)
        {
            //if(file != null && file.ContentLength > 0)
            if (file?.ContentLength > 0)
            {
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);

                var route = Server.MapPath(serverFolderPath);

                route = route + "/" + pictureName;
                file.SaveAs(route);

                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Name = file.FileName;
                //emoPicture.Path = serverFolderPath + "/" + pictureName;
                emoPicture.Path = $"{serverFolderPath}/{pictureName}";



                try {
                    db.EmoPictures.Add(emoPicture);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
                } catch (Exception ex)
                {
                    Console.Write(ex.StackTrace);
                }
            }

            return(View());
        }
Example #5
0
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            //if(file!=null&& file.ContentLength >0)
            if (file?.ContentLength > 0)
            {
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);
                //routea un path de servidor a una ruta local
                var route = Server.MapPath(serverFolderPath);
                route = route + "/" + pictureName;
                file.SaveAs(route);
                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Name = file.FileName;
                //>Guardar un path relativo
                //emoPicture.Path = serverFolderPath + "/" + pictureName;
                //Interpolacion de cadenas
                emoPicture.Path = $"{ serverFolderPath}/{ pictureName }";
                db.EmoPictures.Add(emoPicture);
                await db.SaveChangesAsync();

                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }

            return(View());
        }
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            try
            {
                if (file?.ContentLength > 0)
                {
                    var pictureName = Guid.NewGuid().ToString();
                    pictureName += Path.GetExtension(file.FileName);
                    var route = Server.MapPath(ServerFolderPath);
                    route = route + "/" + pictureName;
                    file.SaveAs(route);
                    var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                    emoPicture.Name = file.FileName;
                    emoPicture.Path = $"{ServerFolderPath}/{pictureName}";

                    db.EmoPictures.Add(emoPicture);
                    await db.SaveChangesAsync();


                    return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
                }
            }
            catch (DbEntityValidationException ex)
            {
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
            catch (DbUpdateException ex)
            {
                //HandleDbUpdateException(ex);

                string mesage = ex.Message;
            }
            return(View());
        }
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            //file != null es lo mismo que decir => file?
            if (file?.ContentLength > 0)
            {
                var pictureName = Guid.NewGuid().ToString();
                pictureName += Path.GetExtension(file.FileName);

                var route = Server.MapPath(serverFolderPath);
                route = route + "/" + pictureName;
                file.SaveAs(route);
                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Nombre = file.FileName;
                //forma de concatenar con llaves, reemplaza a serverfolder + / + picture
                emoPicture.Path = $"{ serverFolderPath} / { pictureName}";
                db.EmoPictures.Add(emoPicture);
                await db.SaveChangesAsync();

                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }
            return(View());
        }
Example #8
0
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            // if(file != null && file.ContentLength> 0)
            if (file?.ContentLength > 0)
            {
                // file.FileName -> nombre del archivo
                // Guid -> randomico
                var pictureName = Guid.NewGuid().ToString();

                // return extencion file
                pictureName += Path.GetExtension(file.FileName);

                // pasear ruta dinamica a la del servidor
                var route = Server.MapPath(serverFolderPath);
                route = route + "/" + pictureName;

                // save file in server -> path in system
                file.SaveAs(route);

                // como es un metodo asyncrono debe tener await
                var emoPicture = await emotionHelper.DetectAndExtractFacesAsync(file.InputStream);

                emoPicture.Nombre = file.FileName;

                // path relativo desde la web
                emoPicture.Path = serverFolderPath + "/" + pictureName;

                db.EmoPictures.Add(emoPicture);
                await db.SaveChangesAsync();

                // redireccione a accion Details en controlador EmoPicture
                // variables a enviar a details
                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }
            return(View());
        }
        public async Task <ActionResult> Index(HttpPostedFileBase file)
        {
            if (file?.ContentLength > 0)
            {
                //Genero una cadena aleatoria de texto
                var pictureName = Guid.NewGuid().ToString();
                //Concateno el nombre y la extension
                pictureName += Path.GetExtension(file.FileName);

                //Agregamos la ruta
                //Server.MapPath genera una ruta en disco de mi servidor
                var route = Server.MapPath(serverFolderPath);
                route = route + "/" + pictureName;

                //Lo guardamos
                file.SaveAs(route);

                //Llama el metodo del Helper
                var emoPicture = await emoHelper.DetectAndExtractFacesAsync(file.InputStream);

                //Agregamos el nombre y la ruta
                emoPicture.Name = file.FileName;
                emoPicture.Path = serverFolderPath + "/" + pictureName;

                //Se agrega
                db.EmoPictures.Add(emoPicture);

                //Y asincronamente se guarda en la Base de Datos
                await db.SaveChangesAsync();

                //Retorna hacia el detalle de la Pintura
                return(RedirectToAction("Details", "EmoPictures", new { Id = emoPicture.Id }));
            }

            return(View());
        }