Esempio n. 1
0
        public bool UploadImages(UploadArticlePhotoModel uploadData)
        {
            Dictionary<string, string> versions = new Dictionary<string, string>();
            //Define the versions to generate
            versions.Add("_indexThumb", "width=361&height=78&crop=auto&format=jpg");
            versions.Add("_BigThumb", "maxwidth=361&height=223&crop=auto&format=jpg");
            versions.Add("_detailsSmallThumb", "width=77&height=61&crop=auto&format=jpg"); //Fit inside 400x400 area, jpeg
            versions.Add("_large", "width=827&crop=auto&format=jpg");

            int ArticleId = uploadData.ArticleId;
            var theArticle = this.Data.Articles.Find(ArticleId);

            foreach (var file in uploadData.Files)
            {
                if (file != null)
                {
                    var originalFileName = file.FileName.Split('.')[0].Replace(' ', '_');
                    var originalFileExtension = file.FileName.Split('.')[1];

                    string uploadFolder = System.Web.HttpContext.Current.Server.MapPath("~/Uploads/Articles/" + ArticleId);
                    if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder);

                    foreach (string suffix in versions.Keys)
                    {
                        string fileName = Path.Combine(uploadFolder, originalFileName + suffix);

                        fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true);
                    }

                    var newImage = new Image
                    {
                        ImagePath = "Uploads\\Articles\\" + ArticleId + "\\" + originalFileName,
                        ImageExtension = originalFileExtension,
                        IsPrimary = true,
                        DateAdded = DateTime.Now,
                    };

                    theArticle.Image = newImage;
                    this.Data.SaveChanges();
                }
            }

            return true;
        }
Esempio n. 2
0
        public ActionResult UploadPhotos(UploadArticlePhotoModel uploadData)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    this.imagesService.UploadImages(uploadData);
                }

                TempData["message"] = "Снимката беше <strong>добавена</strong> успешно!";
                TempData["messageType"] = "success";
                return RedirectToAction("Index");
            }
            catch
            {
                TempData["message"] = "Неуспешно качване на снимка!<br/> Моля свържете се с администратор!";
                TempData["messageType"] = "danger";
                return RedirectToAction("Index");
            }
        }