コード例 #1
0
        public ActionResult Post([FromForm] CreateNewsDto createDto)
        {
            var ext = Path.GetExtension(createDto.Image.FileName); //.gif

            if (!FileUpload.AllowedExtensions.Contains(ext))
            {
                return(UnprocessableEntity("Image extension is not allowed."));
            }

            try
            {
                var newFileName = Guid.NewGuid().ToString() + "_" + createDto.Image.FileName;

                var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName);

                var pathSave = @"/uploads/" + newFileName;

                createDto.Image.CopyTo(new FileStream(filePath, FileMode.Create));

                createDto.Path = pathSave;
                _addNewsCommand.Execute(createDto);
                return(Ok());
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
コード例 #2
0
 public ActionResult Create(CreateNewsDto dto)
 {
     if (!ModelState.IsValid)
     {
         TempData["greska"] = "Doslo je do greske pri unosu";
         RedirectToAction("create");
     }
     try
     {
         _addNewsCommand.Execute(dto);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         TempData["error"]  = "Whoops! Something went wrong!";
         ViewBag.Categories = _getCategoriesCommand.Execute(null);
         return(View());
     }
 }