public JsonResult SaveImagen(string jsonProductoFoto) { if (Request.HasFormContentType) { var form = Request.Form; foreach (var formFile in form.Files) { //PictureViewModels f = (PictureViewModels) JsonConvert.DeserializeObject(jsonProductoFoto); var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; PictureViewModels ConfigModelo = JsonConvert.DeserializeObject <PictureViewModels>(jsonProductoFoto, settings); if (formFile != null) { if (ConfigModelo.idfoto != 0 && ConfigModelo.idproducto != 0 && ConfigModelo.CantidadDeregistros != 0) { var Valida = _IProcesoImagenes.ValidaFotoProducto(ConfigModelo.idfoto, ConfigModelo.idproducto); // solo guarda si se completo el insert de las entidades var PathToSave = Path.Combine(_IEnviroment.WebRootPath, "Imagen"); if (formFile.Length > 0) { if (Valida != false) { using (var fileStream = new FileStream(Path.Combine(PathToSave, ConfigModelo.NombreImagen), FileMode.Create)) { formFile.CopyTo(fileStream); } } } } } } } return(Json(0)); }
public void SendToApi(IFormFile files, string Filename, PictureViewModels PictureViewModels) { try { // Armar un objeto JSON para Validar var URl = _URLApiPath.url.Trim(); int status = 0; if (URl != null) { using (var cliente = new HttpClient()) { cliente.BaseAddress = new Uri(URl); byte[] dataImagen; string outputProductoFoto = JsonConvert.SerializeObject(PictureViewModels); using (var data = new BinaryReader(files.OpenReadStream())) { dataImagen = data.ReadBytes((int)files.OpenReadStream().Length); ByteArrayContent bytes = new ByteArrayContent(dataImagen); MultipartFormDataContent multContent = new MultipartFormDataContent(); multContent.Add(bytes, "file", Filename); HttpResponseMessage resul = cliente.PostAsync("api/CargarImagen?jsonProductoFoto=" + outputProductoFoto + "", multContent).Result; //var re= StatusCode(int.Parse( resul.StatusCode.ToString())); var returnString = resul.Content.ReadAsStringAsync(); } } } } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> AgregarNuevaFoto( IFormFile Files, PictureViewModels PictureViewModels) { string NombreImagenInicial = string.Empty; if (Files != null) { if (Files.FileName.Contains(".jpg") || Files.FileName.Contains(".JPEG") || Files.FileName.Contains(".gif") || Files.FileName.Contains(".png") || Files.FileName.Contains(".tif")) { NombreImagenInicial = Files.FileName.Replace(" ", ""); /////// var Insert_Fotos = new PictureViewModels(); if (PictureViewModels != null) { var GetValue = new BEImagen(); var GetValuep = new BEProducto(); GetValue.descripcion = PictureViewModels.descripcionImagen; GetValue.posicionPortada = PictureViewModels.posicionPortada; GetValuep.Descripcion = PictureViewModels.DescripcionProducto; GetValuep.fechaexpiracion = PictureViewModels.fechaexpiracion; GetValuep.fechaproduccion = PictureViewModels.fechaproduccion; GetValuep.Nombre = PictureViewModels.NombreProducto; GetValuep.Precio = PictureViewModels.Precio; GetValuep.Stock = PictureViewModels.Stock; NombreImagenInicial = PictureViewModels.NombreImagen.Replace(" ", "") + Guid.NewGuid().ToString() + NombreImagenInicial; GetValue.Nombre = NombreImagenInicial; Insert_Fotos = _IFotos.Insert_BEFoto(GetValue, GetValuep); PictureViewModels.idfoto = Insert_Fotos.idfoto; PictureViewModels.idproducto = Insert_Fotos.idproducto; PictureViewModels.CantidadDeregistros = Insert_Fotos.CantidadDeregistros; PictureViewModels.NombreImagen = NombreImagenInicial; } // solo guarda si se completo el insert de las entidades var PathToSave = Path.Combine(_IEnviroment.WebRootPath, "images"); if (Files.Length > 0) { if (Insert_Fotos.CantidadDeregistros > 0) { //grabamos la imange en la APP using (var SaveFile = new FileStream(Path.Combine(PathToSave, NombreImagenInicial), FileMode.Create)) { await Files.CopyToAsync(SaveFile); } //Enviamos A La API La imagen SendToApi(Files, NombreImagenInicial, PictureViewModels); } } } } ///////////////// //var file = Request.; return(RedirectToAction("ProductosLista", "Producto")); }
public async Task <IActionResult> AgregarNuevaFoto( IFormFile Files, PictureViewModels PictureViewModels) { string NombreImagenInicial = string.Empty; string NombreImagen = string.Empty; if (Files.Name.Length >= 5) { if (Files.Name.Contains(".jpg") || Files.Name.Contains(".JPEG") || Files.Name.Contains(".gif") || Files.Name.Contains(".png") || Files.Name.Contains(".tif")) { NombreImagenInicial = Files.Name.Replace(" ", ""); /////// var Insert_Fotos = new PictureViewModels(); if (PictureViewModels != null) { var GetValue = new BEFotos(); var GetValuep = new BEProducto(); GetValue.descripcion = PictureViewModels.descripcionImagen; GetValue.Nombre = PictureViewModels.NombreImagen; GetValue.posicionPortada = PictureViewModels.posicionPortada; GetValuep.Descripcion = PictureViewModels.DescripcionProducto; GetValuep.fechaexpiracion = PictureViewModels.fechaexpiracion; GetValuep.fechaproduccion = PictureViewModels.fechaproduccion; GetValuep.Nombre = PictureViewModels.NombreProducto; GetValuep.Precio = PictureViewModels.Precio; GetValuep.Stock = PictureViewModels.Stock; var Gui = new Guid(); NombreImagen = PictureViewModels.NombreImagen.Replace(" ", "") + Gui.ToString(); Insert_Fotos = _IFotos.Insert_BEFoto(GetValue, GetValuep); } // solo guarda si se completo el insert de las entidades var PathToSave = Path.Combine(_IEnviroment.WebRootPath, "Imagen"); if (Files.Length > 0) { if (Insert_Fotos.CantidadDeregistros > 0) { using (var SaveFile = new FileStream(Path.Combine(PathToSave, NombreImagen + NombreImagenInicial), FileMode.Create)) { await Files.CopyToAsync(SaveFile); } } } } } ///////////////// //var file = Request.; return(View()); }
public PictureViewModels Insert_BEFoto(BEFotos BEImagen, BEProducto BEProducto) { PictureViewModels Cantidadregistros = new PictureViewModels(); int cantidadregistro = 0; using (var conexion = new SqlConnection()) { API_Video_Imagen.Data.Repository.Conecta Conecta = new API_Video_Imagen.Data.Repository.Conecta(); string Conexion = Conecta.GetConexion().ConnectionString; try { var Parameter = new SqlParameter[13]; Parameter[0] = new SqlParameter("@Nombre", SqlDbType.VarChar, 300) { Value = BEImagen.Nombre }; Parameter[1] = new SqlParameter("@rutafoto", SqlDbType.VarChar, 300) { Value = "" }; Parameter[2] = new SqlParameter("@posicionPortada", SqlDbType.Int) { Value = BEImagen.posicionPortada }; Parameter[3] = new SqlParameter("@descripcion", SqlDbType.VarChar, 500) { Value = BEImagen.descripcion }; Parameter[4] = new SqlParameter("@NombreProducto", SqlDbType.VarChar, 500) { Value = BEProducto.Nombre }; Parameter[5] = new SqlParameter("@DescripcionProducto", SqlDbType.VarChar, 600) { Value = BEProducto.Descripcion }; Parameter[6] = new SqlParameter("@Precio", SqlDbType.Decimal) { Value = BEProducto.Precio }; Parameter[7] = new SqlParameter("@Stock", SqlDbType.Int) { Value = BEProducto.Stock }; Parameter[8] = new SqlParameter("@fechaproduccion", SqlDbType.DateTime) { Value = BEProducto.fechaproduccion }; Parameter[9] = new SqlParameter("@fechaexpiracion", SqlDbType.DateTime) { Value = BEProducto.fechaexpiracion }; Parameter[10] = new SqlParameter("@Error", SqlDbType.Bit) { Direction = ParameterDirection.Output }; Parameter[11] = new SqlParameter("@Respuesta", SqlDbType.NVarChar, 200) { Direction = ParameterDirection.Output }; Parameter[12] = new SqlParameter("@CantidadRegistrado ", SqlDbType.Int) { Direction = ParameterDirection.Output }; using (var read = PI_Video_Imagen.Data.SqlHelper.ExecuteReader(Conexion, CommandType.StoredProcedure, "dbo.Insert_Foto", Parameter)) { while (read.Read()) { Cantidadregistros.idfoto = Int64.Parse(read.GetInt64(read.GetOrdinal("IdFoto")).ToString()); Cantidadregistros.CantidadDeregistros = Int32.Parse(read.GetInt32(read.GetOrdinal("CantidadRegistrado")).ToString()); Cantidadregistros.idproducto = Int64.Parse(read.GetInt64(read.GetOrdinal("IdFoto")).ToString()); } } } catch (Exception ex) { throw ex; } } return(Cantidadregistros); }