Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IEnumerable<IFormFile> is not correctly binding #1967

Closed
ranouf opened this issue Sep 27, 2018 · 5 comments
Closed

IEnumerable<IFormFile> is not correctly binding #1967

ranouf opened this issue Sep 27, 2018 · 5 comments

Comments

@ranouf
Copy link

ranouf commented Sep 27, 2018

Hi,

I have an Issue when I try to upload Files from my Angular 6 app to .net core web api.

Here is my C# classes:

public class SampleDetailsDto 
 {
     public Guid Id{ get; set; }
     [Required]
     public string Text { get; set; }
     public string ImageUrl { get; set; }
     public IEnumerable<IFormFile> Files { get; set; }
     public IList<SampleItemDto> SampleItems { get; set; }
 }
 public class SampleItemDto : EntityDto<Guid?>, IAuditedDto, IDeleteAuditedDto
 {
     public Guid Id{ get; set; }
     [Required]
     public string Text { get; set; }
     public string ImageUrl { get; set; }
     public IEnumerable<IFormFile> Files { get; set; }
 }

My C# Controller:

        [Route("{id:guid}")]
        public async Task<IActionResult> UpdateAsync([FromRoute]Guid id, [FromForm] SampleDetailsDto dto)
        {
              Console.Write(dto.Files?.Length); // return null
              return OK();
       }

image
As you can see Files are null.

But if I look in the request:
image

With more details:
image

As we can see the Files are correctly sent to the server, in the name we can see that there are supposed to be correctly bind to the DTO which is not the case.

I read here: aspnet/Mvc#7719 that It s recommended to use IFormCollection, so yes, I can see all the Files uploaded, but they are all in one property called "Files" instead of being linked to SampleDetailsDto or ChildSampleDto.

Did you notice this bug too? Did you find a way to patch it?

@alsami
Copy link

alsami commented Sep 28, 2018

Hi,

this should work just fine. I have create a small sample with a static file here.

@ranouf
Copy link
Author

ranouf commented Oct 1, 2018

Hi,

Thank for your help and to have taken the time to do a repository.
In your example, you test with a list of files. In my situation, I have a more complex object (maybe there is a difference?), a parent and a list of children which both can have a name and a list of files.
I did a repository, so you will be able to test the problem:
https://github.com/ranouf/TestMultipleUpload

Let me know if you have questions :)

@alsami
Copy link

alsami commented Oct 1, 2018

Indeed, does not work, even with nested formdata. I updated the sample. It works fine with normal nested properties, not for nested files though.

I suggest you to move that issue to the MVC Repository.

@ranouf
Copy link
Author

ranouf commented Oct 1, 2018

Thanks @alsami, I created a new issue on MVC Repository. (aspnet/Mvc#8527)

@ranouf
Copy link
Author

ranouf commented Oct 1, 2018

An issue has been found:
aspnet/Mvc#8527 (comment)

@ranouf ranouf closed this as completed Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants