Exemple #1
0
        public void UploadImage(IFormFile image, int id)
        {
            try
            {
                var uploadPath = $"~/uploads/posts/{id}/Image/{image.FileName}";
                try
                {
                    SaveFile(image, "posts", id.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }


                var photo = new Photo
                {
                    IsMain = true,
                    Url    = uploadPath
                };
                _context.Add(photo);
                _context.SaveChanges();

                var postPhoto = new PostsPhoto
                {
                    PostId  = id,
                    PhotoId = photo.Id
                };
                _context.Add(postPhoto);
                _context.SaveChanges();
            }



            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #2
0
        public void UploadImage(List <IFormFile> image, int id)
        {
            try
            {
                for (int i = 0; i < image.Count; i++)
                {
                    var uploadPath = $"~/uploads/posts/{id}/Image/{image[i].FileName}";
                    try
                    {
                        SaveFile(image[i], "posts", id.ToString());
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }


                    var photo = new Photo
                    {
                        IsMain = false,
                        Url    = uploadPath
                    };
                    _context.Add(photo);
                    _context.SaveChanges();

                    var postPhoto = new PostsPhoto
                    {
                        PostId  = id,
                        PhotoId = photo.Id
                    };
                    _context.Add(postPhoto);
                    _context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }