예제 #1
0
        public async Task <IActionResult> Create(BlogDescImage photo)
        {
            if (photo.Files == null || photo.Files.Length == 0)
            {
                ModelState.AddModelError("Files", "Please, select at least 1 file!");
                return(View());
            }

            foreach (IFormFile img in photo.Files)
            {
                if (!img.ContentType.Contains("image/"))
                {
                    ModelState.AddModelError("Files", "You do not have an access for adding different type of file!");
                    return(View());
                }

                var        imgSave    = Path.Combine(_env.WebRootPath, "img");
                var        fileName   = Guid.NewGuid().ToString() + img.FileName;
                var        path       = Path.Combine(imgSave, fileName);
                FileStream fileStream = new FileStream(path, FileMode.Create);

                await img.CopyToAsync(fileStream);

                BlogDescImage newPhoto = new BlogDescImage {
                    Image = fileName
                };

                await _db.AddAsync(newPhoto);

                await _db.SaveChangesAsync();
            }
            return(RedirectToAction("Index"));
        }
 public void UpdateBlogDescImage(BlogDescImage entity)
 {
     context.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
     context.SaveChanges();
 }
 public void AddBlogDescImage(BlogDescImage entity)
 {
     context.BlogDescImages.Add(entity);
     context.SaveChanges();
 }