示例#1
0
        public IActionResult CreateGallery(int id, GalleryViewModel viewModel)
        {
            if (viewModel.Img != null)
            {
                if (Path.GetExtension(viewModel.Img.FileName) != ".jpg")
                {
                    ModelState.AddModelError("Img", "فایل با پسوند jpg بارگزاری شود");
                }
                else
                {
                    string filePath = "";
                    viewModel.ImgName = CodeGenerators.FileCode() + Path.GetExtension(viewModel.Img.FileName);
                    filePath          = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/galleries/", viewModel.ImgName);

                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        viewModel.Img.CopyTo(stream);
                    }

                    ProductGallery productGallery = new ProductGallery()
                    {
                        ProductId = id,
                        Img       = viewModel.ImgName
                    };

                    _store.AddGallery(productGallery);
                }
            }

            ViewBag.MyId = id;
            return(View());
        }