IFormFileCollection represents multiple files uploaded with a request. ASP.NET Core can bind it directly to minimal API handlers or Razor Page handlers. section 7.6 section 16.2.2
Use IFormFileCollection when handling several uploaded files, while keeping these practical concerns in mind:
- Iterate through the collection and use each IFormFile to read uploaded content. section 7.6.3
- Treat filenames as untrusted input and generate a new name before saving a file. section 7.6.3
- Expect buffering in memory and on disk; large uploads may require streaming, which is more complex and error-prone. section 7.6.3
- Decide carefully whether uploaded data belongs in a database, filesystem, or another storage system. section 7.6.3
Binding several files in a minimal API
app.MapGet("/upload", (IFormFileCollection files) => { foreach (IFormFile file in files) { // Read or process each file here } });
app.MapGet("/upload", (IFormFileCollection files) =>
{
foreach (IFormFile file in files)
{
}
});
Browse the minimal API example that receives an IFormFileCollection and iterates over its individual files. section 7.6.3
