Пример #1
0
        public IActionResult CreateChapter(int id, MangaChapterCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Files != null && model.Files.Count > 0)
                {
                    foreach (IFormFile file in model.Files)
                    {
                        string uploadsFolder =
                            Path.Combine(_hostingEnvironment.WebRootPath, "Mangas");
                        string filePath =
                            Path.Combine(uploadsFolder,
                                         id.ToString(),
                                         model.ChapterNo.ToString());
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }

                        string fullPath =
                            Path.Combine(filePath, file.FileName);
                        file.CopyTo(new FileStream(fullPath, FileMode.Create));
                    }
                }

                MangaChapter newChapter = new MangaChapter()
                {
                    ChapterNo = model.ChapterNo,
                    MangaId   = id,
                    Title     = model.Title,
                    ChFolder  =
                        Path.Combine(_hostingEnvironment.WebRootPath,
                                     "Mangas",
                                     id.ToString(),
                                     model.ChapterNo.ToString())
                };
                _mangaRepository.AddChapter(newChapter);
                return(RedirectToAction("Chapters", new { id = id }));
            }

            return(View(model));
        }