public void UploadFile() { if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any()) { // Obtener la imagen subida var httpPostedFile = System.Web.HttpContext.Current.Request.Files["UploadedImage"]; String usuarioId = User.Identity.Name; String tienda = Session["Tienda_Nombre"].ToString(); if (httpPostedFile != null) { byte[] imageBytes = null; using (var binaryReader = new BinaryReader(httpPostedFile.InputStream)) { imageBytes = binaryReader.ReadBytes(httpPostedFile.ContentLength); } ImagenUsuario iu = new ImagenUsuario { UsuarioID = usuarioId, Imagen = imageBytes }; //uC.EliminarImagenUsuario(usuarioId, tienda); uC.AgregarImagenUsuario(iu, tienda); } } }
//--IMAGENES-- public void AgregarImagenUsuario(ImagenUsuario iu, string idTienda) { try { chequearTienda(idTienda); using (var context = ChebayDBContext.CreateTenant(idTienda)) { var qImagen = from img in context.imagenesusuario where img.UsuarioID == iu.UsuarioID select img; if (qImagen.Count() == 0) { //Agregar Imágen context.imagenesusuario.Add(iu); context.SaveChanges(); } else { //Modificar imágen ImagenUsuario imgu = qImagen.FirstOrDefault(); imgu.Imagen = iu.Imagen; context.SaveChanges(); } } } catch (Exception e) { Debug.WriteLine(e.Message); throw e; } }
// To convert the Byte Array to the author Image public FileContentResult getUserImg(String userId) { String tienda = Session["Tienda_Nombre"].ToString(); ImagenUsuario iu = uC.ObtenerImagenUsuario(userId, tienda); if (iu != null) { byte[] byteArray = iu.Imagen; return(byteArray != null ? new FileContentResult(byteArray, "image/jpeg") : null); } else { var dir = Server.MapPath("~/Content/Images"); var path = Path.Combine(dir, "user_sin_imagen.png"); return(new FileContentResult(System.IO.File.ReadAllBytes(path), "image/jpeg")); } }
public ImagenUsuario ObtenerImagenUsuario(string idUsuario, string idTienda) { try { chequearTienda(idTienda); using (var context = ChebayDBContext.CreateTenant(idTienda)) { var qImg = from img in context.imagenesusuario where img.UsuarioID == idUsuario select img; ImagenUsuario ret = qImg.FirstOrDefault(); return(ret); } } catch (Exception e) { Debug.WriteLine(e.Message); throw e; } }