Home > database >  FromForm IFormFile doesn't work in controller
FromForm IFormFile doesn't work in controller

Time:07-19

I'm try receive a file in controller, but he always come without values.

Controller:

[ApiController, Route("api/[controller]")]
public class BlobController : ControllerBase
{
    [HttpPost]
    [Consumes("multipart/form-data")]
    public async Task<ActionResult<FileUploadResponses>> Post([FromForm]IEnumerable<IFormFile> formsfiles)
    {
        List<FileUploadResponse> fileResponseList = new();

        ...

        return Ok(new FileUploadResponses { Files= fileResponseList });
    }
}

Image Controller:

enter image description here

I'm trying with two ways:

PostMan: enter image description here

Blazor HttpClient (follow example MS https://docs.microsoft.com/pt-br/aspnet/core/blazor/file-uploads?view=aspnetcore-6.0&pivots=webassembly):

private async Task UploadFiles(IEnumerable<IBrowserFile> files)
    {
        FileUploadResponses uploadResponses = new();

        using (var content = new MultipartFormDataContent())
        {
            foreach (var file in files)
            {
                var fileContent = new StreamContent(file.OpenReadStream(MaxFileSize));

                fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);

                content.Add(
                    content: fileContent,
                    name: "\"files\"",
                    fileName: file.Name
                );
            }

            var response = await Client.PostAsync("/api/blob", content);

            uploadResponses = await response.Content.ReadFromJsonAsync<FileUploadResponses>() ?? new();
        }
    }

does anyone have a solution to recieve the [FromForm]IEnumerable<IFormFile> with values?

CodePudding user response:

While posting, the data cannot be found because the parameter name is written as file, you should try it as formFiles in postman.

  • Related