Example #1
0
        public async Task <ResponseBase> InsertarImagenProducto(Imagenes_Producto ip)
        {
            var response = new ResponseBase();

            try
            {
                using (var connection = new SqlConnection(con.getConnection()))
                {
                    using (var command = new SqlCommand("Reporte.spInsertarImagenProducto", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@idImagen", 0);
                        command.Parameters.AddWithValue("@idEmpresa", ip.idEmpresa);
                        command.Parameters.AddWithValue("@imagen", ip.imagen);
                        command.Parameters.AddWithValue("@url", ip.url);
                        command.Parameters.AddWithValue("@idProducto", ip.idProducto);
                        command.Parameters["@idImagen"].Direction = ParameterDirection.Output;
                        connection.Open();
                        var result = await command.ExecuteNonQueryAsync();

                        if (result > 0)
                        {
                            response.id      = Convert.ToInt32(command.Parameters["@idImagen"].Value);
                            response.success = true;
                            response.message = "Datos Insertados Correctamente";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.success = false;
                response.message = ex.Message;
            }
            return(response);
        }
Example #2
0
        public async Task <ActionResult> ImagenProducto(IFormCollection formdata)
        {
            var           response      = new ResponseBase();
            var           imgp          = new Imagenes_Producto();
            List <string> fileExtension = new List <string>()
            {
                ".png", ".jpg", ".jpeg"
            };

            try
            {
                imgp.idProducto = int.Parse(formdata["idProducto"]);
                var idEmpresa = formdata["idEmpresa"];

                var files = HttpContext.Request.Form.Files;

                foreach (var file in files)
                {
                    var    filename = file.FileName;
                    var    ext      = Path.GetExtension(filename);
                    var    buffer   = file.Length;
                    double mb       = (buffer / 1024f) / 1024f;
                    if (mb > 20)
                    {
                        response.success = false;
                        response.message = "tamaƱo de archivo demasiado grande";
                        return(StatusCode(500, response));
                    }
                    if (!fileExtension.Contains(ext))
                    {
                        response.success = false;
                        response.message = "extension de archivo no permitida";
                        return(StatusCode(500, response));
                    }
                    if (file.Length > 0)
                    {
                        var filee    = Path.Combine(environment.ContentRootPath);
                        var date     = DateTime.Now;
                        var id       = date.Millisecond;
                        var carpeta  = filee + "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + imgp.idProducto;
                        var filePath = filee + "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + imgp.idProducto + "\\" + id + ext;
                        var ruta     = "\\" + "Content" + "\\" + "empresa" + "\\" + idEmpresa + "\\" + imgp.idProducto + "\\" + id + ext;
                        imgp.url       = ruta;
                        imgp.idEmpresa = int.Parse(idEmpresa);
                        imgp.imagen    = id.ToString() + ext;


                        if (!Directory.Exists(carpeta))
                        {
                            Directory.CreateDirectory(carpeta);
                        }

                        using (var stream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(stream);

                            response = await this.producto.InsertarImagenProducto(imgp);

                            if (response.success == false)
                            {
                                return(StatusCode(403, response));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.success = false;
                response.message = ex.Message;
                return(StatusCode(500, response));
            }
            return(Ok(response));
        }