Exemplo n.º 1
0
        public ActionResult MutipleImages(MultipleImagesModel imagesFiles)
        {
            var startDate = imagesFiles.images.TimeFrom;

            //Ensure model state is valid
            if (ModelState.IsValid && imagesFiles.dayInterval > 0)
            {
                int count = 0;
                foreach (HttpPostedFileBase file in imagesFiles.files.files)
                {
                    if (file != null)
                    {
                        var InputFileName  = Path.GetFileName(file.FileName);
                        var ServerSavePath = Path.Combine(Server.MapPath(pathTempImages) + InputFileName);
                        //Save file to server folder
                        file.SaveAs(ServerSavePath);
                        //Add a images
                        var images = new ImageModel();
                        //Store images obj
                        if (Session["Images"] != null)
                        {
                            imagesList      = (List <ImageModel>)Session["Images"];
                            images.ID       = imagesList.LastOrDefault().ID + 1;
                            images.TimeFrom = (count == 0) ? startDate : imagesList.LastOrDefault().TimeTo.AddMinutes(1);
                        }
                        else
                        {
                            imagesList      = new List <ImageModel>();
                            images.ID       = 1;
                            images.TimeFrom = startDate;
                        }
                        count++;
                        images.TimeTo = images.TimeFrom.AddDays(imagesFiles.dayInterval);
                        images.Type   = ((int)Session["Type"] == 1) ? "ScreenSaver" : "LockScreen";
                        images.Name   = InputFileName;
                        images.Url    = pathTempImages + InputFileName;
                        //checkTime
                        var checkTimeResult = CheckTime(1, images.ID, images.TimeFrom, DateTime.MaxValue, images.Type);
                        if (checkTimeResult == "Oke")
                        {
                            imagesList.Add(images);
                            Session["Images"] = imagesList;
                            Notification("Notify", "Upload file successfuly", MyConstants.NOTIFY_SUCCESS);
                        }
                        else
                        {
                            Notification("Error", checkTimeResult, MyConstants.NOTIFY_ERROR);
                        }
                    }
                    else
                    {
                        Notification("Error", "No files being selected", MyConstants.NOTIFY_ERROR);
                    }
                }
            }
            else
            {
                Notification("Error", "Please recheck infomation", MyConstants.NOTIFY_ERROR);
            }
            ViewBag.Type = ((int)Session["Type"] != 2) ? "ScreenSaver" : "LockScreen";
            if (Session["Images"] != null)
            {
                var sessionCart = (List <ImageModel>)Session["Images"];
                ViewBag.listImages = sessionCart.Where(x => x.Type == ViewBag.Type).ToList();
            }
            ModelState.Clear();
            return(View("Index"));
        }
Exemplo n.º 2
0
        public ActionResult UploadMultipleImages(MultipleImagesModel imagesFiles)
        {
            //Ensure model state is valid
            bool check = true;

            if (ModelState.IsValid)
            {
                try
                {
                    foreach (HttpPostedFileBase file in imagesFiles.files.files)
                    {
                        if (file != null)
                        {
                            var InputFileName  = Path.GetFileName(file.FileName);
                            var ServerSavePath = Path.Combine(Server.MapPath(pathTempImages) + InputFileName);
                            //Save file to server folder
                            file.SaveAs(ServerSavePath);
                            //Add a images
                            var images = new ImageModel();
                            //Store images obj
                            if (Session["Images"] != null)
                            {
                                imagesList = (List <ImageModel>)Session["Images"];
                                images.ID  = imagesList.LastOrDefault().ID + 1;
                            }
                            else
                            {
                                imagesList = new List <ImageModel>();
                                images.ID  = 1;
                            }
                            images.TimeFrom = imagesFiles.images.TimeFrom;
                            images.TimeTo   = imagesFiles.images.TimeTo;
                            //Check time here
                            images.Type = "ScreenSaver";
                            images.Name = InputFileName;
                            images.Url  = pathTempImages + InputFileName;
                            //checkTime
                            //var checkTimeResult = CheckTime(1, images.ID, images.TimeFrom, DateTime.MaxValue, images.Type);
                            if (images.TimeFrom < images.TimeTo && images.TimeFrom > DateTime.Now)
                            {
                                imagesList.Add(images);
                                Session["Images"] = imagesList;
                            }
                            else
                            {
                                Notification("Error", "TimeStart must greater than TimeNow and less than TimeEnd", MyConstants.NOTIFY_ERROR);
                                check = false;
                            }
                        }
                        else
                        {
                            Notification("Error", "No files being selected", MyConstants.NOTIFY_ERROR);
                            check = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Notification("Error", "Please check time correct!", MyConstants.NOTIFY_ERROR);
                    check = false;
                }
            }
            else
            {
                Notification("Error", "Please recheck infomation", MyConstants.NOTIFY_ERROR);
                check = false;
            }
            if (check == true)
            {
                Notification("Notify", "Upload file successfuly", MyConstants.NOTIFY_SUCCESS);
            }
            ViewBag.Type = "ScreenSaver";
            var sessionCart = (List <ImageModel>)Session["Images"];

            if (sessionCart != null)
            {
                ViewBag.listImages = sessionCart.Where(x => x.Type == "ScreenSaver").ToList();
            }
            ModelState.Clear();
            return(View("Index"));
        }