5

I need to send multiple files to ASP.net core webApi method.I have tried as shown below.But it always shows as 0 files.Can you tell me why ?

[Route("api/[controller]/[action]")]
[Consumes("application/json", "application/json-patch+json", "multipart/form-data")]
public class DocumentUploadController : CpcpControllerBase
{
    [HttpPost]
    public async Task<List<string>> AddDocument(ICollection<IFormFile> files)
    {
        foreach (var f in files)
        {
            var stream = f.OpenReadStream();
            var name = f.FileName;
        }
    }
}

Postman :

enter image description here

But I can send 1 file as shown below.It's working fine.

[HttpPost]
public async Task<string> AddDocument(IFormFile file)
{
        var stream = file.OpenReadStream();
        var name = file.FileName;           
}
1
  • no difference. same result. that too shows as 0 files. @Set Commented Jan 17, 2017 at 21:11

3 Answers 3

8

Replace key file1 by files in postman. It works for me.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes,that is it.Thanks a lot :)
Hmm. I can only get this to work with param type of IFormFile. If I switch to ICollection<IFormFile> (or list or array), then I start getting "The input was not valid." back from the ASP.NET Core service.
@bugged87 i am facing the same issue as you ... did you solve it?
@User7291 see my posted answer.
3

Use IFormFileCollection instead of ICollection<IFormFile> per docs.

Comments

0

Replace key file1 by files in postman. Also we can also access files from Http Context as:

var files = HttpContext.Request.Form.Files;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.