Esempio n. 1
0
        public ResponseModel GuardarPerfil(HttpPostedFileBase Foto)
        {
            var rm = new ResponseModel();

            try
            {
                using (var db = new ModeloDatos())
                {
                    db.Configuration.ValidateOnSaveEnabled = false;

                    var Usu = db.Entry(this);

                    Usu.State = EntityState.Modified;

                    if (Foto != null)
                    {
                        string extension       = Path.GetExtension(Foto.FileName).ToLower();
                        int    size            = 1024 * 1024 * 7; //7 megas
                        var    filtroextension = new[] { ".jpg", ".jpeg", ".png", ".gif" };
                        var    extensiones     = Path.GetExtension(Foto.FileName);

                        if (filtroextension.Contains(extensiones) && (Foto.ContentLength < size))
                        {
                            string archivo = Path.GetFileName(Foto.FileName);
                            Foto.SaveAs(HttpContext.Current.Server.MapPath("~/Server/Images/" + archivo));
                            this.avatar = archivo;
                        }
                    }
                    else
                    {
                        Usu.Property(x => x.avatar).IsModified = false;
                    }
                    if (this.usuario_id == 0)
                    {
                        Usu.Property(x => x.usuario_id).IsModified = false;
                    }
                    if (this.persona_id == 0)
                    {
                        Usu.Property(x => x.persona_id).IsModified = false;
                    }
                    if (this.tipousuario == 0)
                    {
                        Usu.Property(x => x.tipousuario).IsModified = false;
                    }
                    if (this.clave == null)
                    {
                        Usu.Property(x => x.clave).IsModified = false;
                    }
                    if (this.estado == null)
                    {
                        Usu.Property(x => x.estado).IsModified = false;
                    }
                    db.SaveChanges();
                    rm.SetResponse(true);
                }
            }
            catch (DbEntityValidationException e)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            return(rm);
        }