Example #1
0
        public IActionResult DeleteProductConfirm(int id)
        {
            using (DatabaseContext db = new DatabaseContext())
            {
                var product = new Products
                {
                    idProducts = id
                };

                db.Products.Remove(product);

                //eliminar del azure
                eliminarImg(id);

                var imgs = new ProductsImg
                {
                    idProducto = id
                };

                db.ProductsImg.RemoveRange(db.ProductsImg.Where(x => x.idProducto == id));


                db.SaveChanges();
                TempData["successDelete"] = 1;
            }
            return(RedirectToAction("Products"));
        }
Example #2
0
        public void SaveImages(IFormFile fileSelect, int id)
        {
            string url = Azure.url;
            string urlFinal;

            //Subir a Azure
            string conn = Azure.conn;
            string fileName;

            BlobServiceClient   blobServiceClient = new BlobServiceClient(conn);
            BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient("productsimage");

            if (fileSelect != null)
            {
                using (var fileStream = fileSelect.OpenReadStream())
                {
                    var    fileArray = fileSelect.FileName.Split('.');
                    string extFile   = fileArray[1];
                    fileName = $"{DateTime.Now.Ticks}.{extFile}";
                    BlobClient blobClient = containerClient.GetBlobClient(fileName);
                    urlFinal = url + fileName;


                    blobClient.Upload(fileStream, true);
                }

                try
                {
                    using (DatabaseContext db = new DatabaseContext())
                    {
                        ProductsImg img = new ProductsImg();

                        img.idProducto = id;
                        img.ImageUrl   = urlFinal;
                        img.Nombre     = fileName;


                        db.ProductsImg.Add(img);

                        int filasAfectadas = db.SaveChanges();
                        if (filasAfectadas > 0)
                        {
                            Console.WriteLine("Agregado exitosamente");
                        }
                        else
                        {
                            ViewBag.err = 1;
                            ViewBag.msj = "error al agregar imagenes a db consulta";
                        }
                    };
                }
                catch (Exception ex)
                {
                    ViewBag.err = 1;
                    ViewBag.msj = "error al agregar imagenes a db";
                }
            }
        }