jasontaylordev / CleanArchitecture

Clean Architecture Solution Template for ASP.NET Core
MIT License
16.33k stars 3.5k forks source link

IFromFile Shows null. #170

Closed altumcon closed 3 years ago

altumcon commented 4 years ago

Can any one please show me how to use ifromfile to upload images. As in my controller when I call for Ifromfile Its returns null. Is there any specification I missing or what? Below is my code for controller, command and command handler controller:

        [HttpPost]
        public  Task<ActionResult<long>> Create([FromForm (Name ="")] CreateNewCommand command)
        {
             return await Mediator.Send(command);
           // return null;
        } 

command

public class CreateNewCommand : IRequest<long>
    {
        public long NewsDetailId { get; set; }
        public string NewsTitle { get; set; }
        public string NewsDescription { get; set; }
        public string NewsExcerpt { get; set; }
        [NotMapped]
        public IFormFile NewsImage { get; set; }

        public bool IsPublished { get; set; }
    }

command handler

 public class CreateNewsCommandHandler : IRequestHandler<CreateNewCommand, long>
    {
        private readonly IApplicationDbContext _context;
        private readonly IMapper _mapper;
        private readonly IMediaService _mediaService;

        public CreateNewsCommandHandler(IApplicationDbContext context, IMapper mapper, IMediaService mediaService)
        {
            _context = context;
            _mapper = mapper;
            _mediaService = mediaService;
        }

        public async Task<long> Handle(CreateNewCommand request, CancellationToken cancellationToken)
        {
            var newsDetails = new NewsDetail();

            newsDetails = _mapper.Map<NewsDetail>(request);
            if (request.NewsImage != null)
            {
                var fileName = await SaveFile(request.NewsImage);
                if (newsDetails.NewsMedia != null)
                {
                    newsDetails.NewsMedia.FileName = fileName;
                }
                else
                {
                    newsDetails.NewsMedia = new Media { FileName = fileName };
                }
            }
            _context.NewsDetails.Add(newsDetails);
            await _context.SaveChangesAsync(cancellationToken);
            return newsDetails.NewsDetailId;
        }
        private async Task<string> SaveFile(IFormFile file)
        {
            var originalFileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            var fileName = $"{Guid.NewGuid()}{Path.GetExtension(originalFileName)}";
            await _mediaService.SaveMediaAsync(file.OpenReadStream(), fileName, file.ContentType);
            return fileName;
        }
    }

I am using NSwag v13.6.1 Please help me.I am really stuck in this

jasontaylordev commented 3 years ago

Hi @altumcon - you will get more help with this kind of issue on StackOverflow. Good luck. 👍

karlbal commented 3 years ago

Hi @altumcon ,

Not sure if you're able to make it work. But I'll just share what I had on my side. I had a similar code with you. But I changed a little on the controller side. By removing the (Name="")

[HttpPost] [Route("updateuserprofilecommand")] public async Task<ActionResult<int>> UpdateUserProfileCommand([FromForm] UpdateUserProfileCommand command) { return await Mediator.Send(command); }

below is the sample payload:

image