AddProduct([FromF" /> AddProduct([FromF" /> AddProduct([FromF"/>

Problem to upload several images per one request

12 Views Asked by At

I have the following controller's action:

    [HttpPost("api/v1/addProduct")]
    [Consumes("multipart/form-data")]
    public Task<ProductDto?> AddProduct([FromForm] ProductRequest request, CancellationToken cancellationToken)
    {
        return _productService.AddProduct(request, cancellationToken);
    }

and the following model:

public class ProductRequest
{
    [Required]
    public string Title { get; set; }

    public string? Description { get; set; }

    [Required]
    public string PreviewDescription { get; set; }

    [Required]
    public IFormFile PreviewImage { get; set; }

    public IFormFile[]? Images { get; set; }

    [Required]
    public int[] CategoryIds { get; set; }
    
    public bool? IsAvailable { get; set; }
}

I successfully sent data to the server (I use formData):

enter image description here

But by some reason, I see the following in the debugger:

enter image description here

Everything is ok with previewImage but where are images? Why previewImage is here, but there is no images? I sent them in exactly the same way as I sent previewImage. We can see it at the request payload screen. Help me please to figure it out.

UPDATE

I tried IFormFileCollection:

enter image description here

No result.

But:

enter image description here

As we can see here (using HttpContext.Request.Form.Files) I see all the files. What is wrong with binding?

I can solve it in this way:

enter image description here

But it looks awful. Model binding must work here! Why it doesn't?

1

There are 1 best solutions below

0
Aleksej_Shherbak On

Ok I found the solution. I used wrong name for images. I have to use images instead of images[]:

enter image description here

This is a bit strange, because as you can see I have categoryIds[] it is an array, and it works as expected.

Now I see:

enter image description here