Example #1
0
        public async Task <IActionResult> Upload(ImageUploadVM viewModel)
        {
            bool   success  = false;
            string fileName = string.Empty;

            if (viewModel.File != null)
            {
                fileName = fileService.GetFileName(viewModel.File.FileName);

                if (!string.IsNullOrEmpty(fileName))
                {
                    success = await fileService.SaveFileFromFormAsync(viewModel.File, fileName);
                }
            }
            else if (viewModel.ImageUrl != null)
            {
                fileName = fileService.GetFileName(viewModel.ImageUrl);

                if (!string.IsNullOrEmpty(fileName))
                {
                    success = await fileService.SaveFileFromUrlAsync(viewModel.ImageUrl, fileName);
                }
            }
            else
            {
                ModelState.AddModelError(nameof(viewModel.ImageUrl),
                                         "At least one file need to be specified.");
                ModelState.AddModelError(nameof(viewModel.File),
                                         "At least one file need to be specified.");
            }

            if (!success)
            {
                ModelState.AddModelError(nameof(viewModel.SomethingWentWrong),
                                         "Something went wrong");
                viewModel.SomethingWentWrong = true;
            }

            if (!ModelState.IsValid)
            {
                viewModel.Galleries = service.GetAllGalleries();
                return(View(viewModel));
            }

            viewModel.FileName = fileName;
            await service.CreateImage(viewModel);

            return(RedirectToAction(nameof(GalleryController.Index), Gallery));
        }