Sampath
Sampath

Reputation: 65870

Send multiple files using ASP.net core web api

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;           
}

Upvotes: 5

Views: 10389

Answers (3)

mohammadAli
mohammadAli

Reputation: 445

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

var files = HttpContext.Request.Form.Files;

Upvotes: 0

bugged87
bugged87

Reputation: 3142

Use IFormFileCollection instead of ICollection<IFormFile> per docs.

Upvotes: 3

Janak
Janak

Reputation: 565

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

Upvotes: 8

Related Questions