Пример #1
0
        public async Task <IActionResult> ChangeMobileImageOrder(IEnumerable <int?> myIds)
        {
            int order = 0;
            List <MobileHomeSliderImages> homeSliderImages = new List <MobileHomeSliderImages>();

            foreach (int?item in myIds)
            {
                if (item == null)
                {
                    return(Json(data: false));
                }
                MobileHomeSliderImages objProductImage = await _db.MobileHomeSliderImages.FindAsync(item);

                objProductImage.ImageOrder = order;
                homeSliderImages.Add(objProductImage);

                order++;
            }
            _db.MobileHomeSliderImages.UpdateRange(homeSliderImages);
            await _db.SaveChangesAsync();

            return(Json(data: true));
        }
Пример #2
0
        public async Task <IActionResult> Upload(List <IFormFile> HomeSliderImages, int homeId)
        {
            //Create images/mobilehomepageslider/medium Folder
            string mediumPath = System.IO.Path.Combine(_host.WebRootPath, "images/mobilehomepageslider", "medium");

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


            //Create images/mobilehomepageslider/small Folder
            string smallPath = System.IO.Path.Combine(_host.WebRootPath, "images/mobilehomepageslider", "small");

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

            try
            {
                List <MobileHomeSliderImages> homeSliderImages = new List <MobileHomeSliderImages>();
                foreach (var item in HomeSliderImages)
                {
                    var imageName = "_" + System.DateTime.Now.ToString("YYYYMMDDhhmmssms") + Path.GetFileName(item.FileName);
                    //path of medium image:
                    var newMediumPath      = System.IO.Path.Combine(_host.WebRootPath, "images/mobilehomepageslider/medium");
                    var finalNewMediumPath = System.IO.Path.Combine(newMediumPath, imageName);

                    //path of small image:
                    var newSmallPath      = System.IO.Path.Combine(_host.WebRootPath, "images/mobilehomepageslider/small");
                    var finalNewSmallPath = System.IO.Path.Combine(newSmallPath, imageName);

                    System.GC.Collect();
                    System.GC.WaitForPendingFinalizers();


                    using (var webPFileStream = new FileStream(finalNewMediumPath, FileMode.Create))
                    {
                        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                        {
                            imageFactory.Load(item.OpenReadStream())
                            .Resize(new Size(800, 0))
                            .Save(webPFileStream);
                        }
                    }

                    using (var webPFileStream = new FileStream(finalNewSmallPath, FileMode.Create))
                    {
                        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                        {
                            imageFactory.Load(item.OpenReadStream())
                            .Resize(new Size(400, 0))
                            .Save(webPFileStream);
                        }
                    }

                    var myimage = new MobileHomeSliderImages();
                    myimage.HomePageId = homeId;
                    myimage.ImageName  = imageName;
                    myimage.ImageOrder = await _db.MobileHomeSliderImages
                                         .AsNoTracking()
                                         .Where(c => c.HomePageId == homeId).CountAsync();

                    homeSliderImages.Add(myimage);
                }
                await _db.MobileHomeSliderImages.AddRangeAsync(homeSliderImages);

                await _db.SaveChangesAsync();

                return(Json(true));
            }
            catch
            {
                return(Json(false));
            }
        }