コード例 #1
0
        public async Task <IActionResult> Create()
        {
            BlogViewModel model = new BlogViewModel();
            await model.Initialize(_categoryCollection, HttpContext);

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Create(BlogViewModel model)
        {
            string BUCKET_NAME = "srx-blog-images";

            if (ModelState.IsValid)
            {
                UserContextEntity user = new UserContextEntity()
                {
                    UserId = model.UserId, UserName = model.UserName, UserPicPath = model.UserPicPath
                };
                var    blog           = new Entities.BlogEntity();
                string uniqueFileName = null;

                if (model.ImageFile != null)
                {
                    var response = new PutObjectResponse();
                    var client   = new AmazonS3Client(RegionEndpoint.EUWest1);
                    uniqueFileName = Guid.NewGuid().ToString() + '_' + model.ImageFile.FileName;
                    using (var stream = new MemoryStream())
                    {
                        model.ImageFile.CopyTo(stream);

                        var request = new PutObjectRequest
                        {
                            BucketName  = BUCKET_NAME,
                            Key         = uniqueFileName,
                            InputStream = stream,
                            ContentType = model.ImageFile.ContentType,
                        };

                        response = await client.PutObjectAsync(request);
                    };
                }

                blog.BlogImage = uniqueFileName;
                blog.IsDeleted = false;
                blog.Like      = new List <UserContextEntity>();
                blog.DisLike   = new List <UserContextEntity>();
                blog.Ratings   = new List <string>();
                blog.SubTitle  = model.SubTitle;
                blog.Title     = model.Title;
                List <string> list = model.Tags.Split(",").ToList();
                blog.Tags = new List <string>();
                foreach (string str in list)
                {
                    blog.Tags.Add(str.Trim());
                }
                blog.Category  = model.Category;
                blog.CreatedBy = user;
                blog.CreatedOn = DateTime.Now;
                blog.Content   = model.Content;
                await _blogService.Create(blog);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                await model.Initialize(_categoryCollection, HttpContext);

                return(View(model));
            }
        }