Exemple #1
0
        public JsonResult Upload(UploadAlbumModel m)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    #region Upload

                    if (m.ImageFiles[0] == null)
                        ModelState.AddModelError("", "Bạn phải chọn ít nhất một hình ảnh!");
                    else if (m.ImageFiles.Count > 100)
                        ModelState.AddModelError("", "Bạn chỉ được đăng tối đa 100 ảnh trong một album!");
                    else
                    {
                        var album = new CreateAlbumRequestModel
                            {
                                AlbumContent = m.AlbumContent,
                                AlbumTitle = m.AlbumTitle,
                                CategoryId = m.CategoryId,
                                MemberId = SessionManager.UserLogged.UserId,
                                Tags = m.Tags,
                                ListImage = new List<CreateAlbumRequestModel.ImageRequest>()
                            };

                        foreach (var img in m.ImageFiles)
                        {
                            if (img.ContentType.Contains("image"))
                            {
                                var memoryStream = new MemoryStream();
                                img.InputStream.CopyTo(memoryStream);
                                album.ListImage.Add(new CreateAlbumRequestModel.ImageRequest()
                                    {
                                        ImageFile = Convert.ToBase64String(memoryStream.ToArray()),
                                        ImageType = img.ContentType,
                                        ImageName = img.FileName,
                                        ImageTags = album.Tags,
                                        ImageStory = album.AlbumContent,
                                        ImageTitle = album.AlbumTitle,
                                        DeviceInfo = "web"
                                    });
                            }
                        }
                        var data1 = PostRepository.CreateAlbum(album);

                        if (data1.Code == "-1")
                            ModelState.AddModelError("", data1.Message);
                        else
                            return Json(new { Url = Url.Action("Detail", new { id = data1.Message }), Result = true });
                    }
                    #endregion
                }

                var message = ModelState.Values.Where(item => item.Errors.Count > 0).Aggregate("", (current1, item) => item.Errors.Aggregate(current1, (current, error) => current + (error.ErrorMessage + "</br>")));
                return Json(new {Result = false , Message = message});

            }
            catch (Exception exception)
            {
                Logger.Error(exception.ToString);
                return Json(new { Result = false, Message = exception.Message });
            }
        }
Exemple #2
0
        public ActionResult Upload()
        {
            try
            {
                var model = new UploadAlbumModel();

                var cate = PostRepository.GetCategories();

                var data = new List<SelectListItem>();
                for (int i = 0; i < cate.Categories.Count; i++)
                {
                    if (i == 0)
                    {
                        data.Add(new SelectListItem()
                            {
                                Selected = true,
                                Text = cate.Categories[i].CategoryName,
                                Value = cate.Categories[i].CategoryId
                            });
                    }
                    else
                    {
                        data.Add(new SelectListItem()
                        {
                            Text = cate.Categories[i].CategoryName,
                            Value = cate.Categories[i].CategoryId
                        });
                    }
                }

                model.Categories = data;

                return View(model);
            }
            catch (Exception exception)
            {
                Logger.Error(exception.ToString());
                return RedirectToAction("Index");
            }
        }