コード例 #1
0
        public IActionResult BadgeEdit(string id, DataAccess.Badge badge)
        {
            if (ModelState.IsValid)
            {
                string webRootPath = _hostEnvironment.WebRootPath;
                var    files       = HttpContext.Request.Form.Files;

                ObjectId oId    = new ObjectId(id);
                var      badges = _badge.FindById(id);
                badge.Id = new ObjectId(id);

                if (files.Count > 0)
                {
                    string fileName   = Guid.NewGuid().ToString();
                    var    uploads    = Path.Combine(webRootPath, @"images/badges");
                    var    extenstion = Path.GetExtension(files[0].FileName);

                    if (badge.ImageUrl != null)
                    {
                        //this is an edit and we need to remove old image
                        var imagePath = Path.Combine(webRootPath, badge.ImageUrl.TrimStart('\\'));
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }
                    }
                    using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create))
                    {
                        files[0].CopyTo(filesStreams);
                    }
                    badge.ImageUrl = @"\images\badges\" + fileName + extenstion;
                }
                else
                {
                    //update when they do not change the image
                    if (badge.Id != null)
                    {
                        badge.ImageUrl = badges.ImageUrl;
                    }
                }



                _badge.ReplaceOne(badge);


                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #2
0
        public IActionResult Create(string Select, DataAccess.Badge badge)
        {
            if (ModelState.IsValid)
            {
                double d = Math.Ceiling(badge.ExpiryDuration);
                switch (Select)
                {
                case "Days":
                {
                    badge.ExpiryDuration = d;
                }
                break;

                case "Week":
                {
                    badge.ExpiryDuration = d * 7;
                }
                break;

                case "Months":
                {
                    badge.ExpiryDuration = d * 30;
                }
                break;

                case "Years":
                {
                    badge.ExpiryDuration = d * 365;
                }
                break;
                }

                string webRootPath = _hostEnvironment.WebRootPath;
                var    files       = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    string fileName   = Guid.NewGuid().ToString();
                    var    uploads    = Path.Combine(webRootPath, @"Images\badges");
                    var    extenstion = Path.GetExtension(files[0].FileName);


                    using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create))
                    {
                        files[0].CopyTo(filesStreams);
                    }



                    badge.ImageUrl = @"\images\badges\" + fileName + extenstion;
                }

                DataAccess.Badge b = new DataAccess.Badge()
                {
                    BadgeName                  = badge.BadgeName,
                    ImageUrl                   = badge.ImageUrl,
                    ExpiryDuration             = badge.ExpiryDuration,
                    EarningCriteriaDescription = badge.EarningCriteriaDescription,
                    CreatedDate                = DateTime.Now,
                    FacebookId                 = badge.FacebookId
                };
                _badge.InsertOne(b);
                return(RedirectToAction("Index"));
            }

            return(View());
        }