0

The following CreateImages endpoint is in my C# .NET Web API. Testing with Postman and Swagger UI, when I upload files and send the request, files parameter below is always null. I don't have this issue if I update the parameter to a single IFormFile, it is only when I try to pass a list.

public class Images : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this)
            .DisableAntiforgery()
            .RequireAuthorization()
            .MapPost(CreateImages);
    }

    public async Task<IResult> CreateImages(ISender sender, [FromForm] IList<IFormFile> files)
    {
        return Results.Ok(await sender.Send(new CreateImagesCommand(files)));
    }
}

I get the same issue with the following types: IList, List, ICollection, IEnumerable. If I pass an array (IFormFile[]), even with the [FromForm] tag, It will not be registered as multipart/form-data but expects a list of strings. I assume this is not recommended anyways. Also, as mentioned, if I update this endpoint to instead accept a single IFormFile, everything works as expected - value is not null and debugger can proceed with item as expected IFormFile type.

enter image description here

3
  • This question is similar to: Submitting multiple files to ASP.NET controller accepting an ICollection<IFormFile>. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 6, 2024 at 16:45
  • The question you linked refers to a problem where an ICollection of IFormFile is being received as empty, not null. Also, that a single IFormFile is being received by the endpoint as an ICollection of IFormFile with one valid item. In my case, a single IFormFile is not being received as a collection, but a valid singular item. The issue on this question was also related to the asker's implementation of Fetch API, which was building the form data and calling the endpoint. I am solely trying to get a backend working, and only testing this endpoint with Postman and Swagger UI. Commented Oct 6, 2024 at 17:01
  • Can you provide the screenshot on how you send multiple files in Postman? I suspect that the request body parameter that you constructed for files is incorrect. Commented Oct 6, 2024 at 23:57

2 Answers 2

1

After playing around with this, it seems Minimal API expects only IFormFileCollection as a valid type for multiple file uploads. Changing the parameter will resolve the issue.

For anyone else who wants to try, spin up an empty project in .NET 8.0 and add the endpoint using Minimal API below in Program.cs. Try different types for the files parameter; I tried ICollection, IList, List, IReadOnlyCollection. All of these fail with 415 Unsupported Type.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// POST endpoint for multiple file upload
app.MapPost("/upload-multiple", async (IFormFileCollection files) =>
{
    if (files.Any())
    {
        foreach (var file in files)
        {
            var filePath = Path.Combine("uploads", file.FileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
        }

        return Results.Ok("Files uploaded successfully.");
    }

    return Results.BadRequest("No files uploaded.");
}).DisableAntiforgery();

app.Run();

Not sure if this is specific to later .NET versions, or if this is mentioned in the docs anywhere that I couldn't find.

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

Comments

0

I think this is due to model binding mechanism. In the document for guiding how to upload files, we can see description like below.

When uploading files using model binding and IFormFile, the action method can accept:

A single IFormFile. Any of the following collections that represent several files: IFormFileCollection IEnumerable List

However, in the document for minimal API model binding, it says:

Binding from form-based parameters using IFormCollection, IFormFile, and IFormFileCollection is supported.

The description can match your test result and what you did in minimal API is correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.