question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IFormFile binding error

See original GitHub issue

Describe the bug

I have a model with IFormFIle property and other simple type properties.It could pass to the controller with [ApiController] and [FromForm] successfully.But If I only pass IFormFile property in model,it could not pass data to controller.I find a solution,when I remove [ApiController] and [FromForm],it works.

To Reproduce

using asp.net core 3.1

Model:

    public class ProductImageDetailsDto
    {
        public IFormFile ProductImage { get; set; }

        public bool IsDefaultImage { get; set; }

    }

Controller:

    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        [HttpPut("UpdateProductImagesByProductId/{id}")]
        public async Task<IActionResult> UpdateProdutImagesByProductId(Guid id,[FromForm]List<ProductImageDetailsDto> productImages)
        {

            return Ok();
        }
    }

SO Reference:

https://stackoverflow.com/questions/65085522/how-to-upload-multiple-files-using-multipart-form-data-media-type-in-net-core-w/65087114

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
daping123commented, Dec 2, 2020

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project (ideally a GitHub repo) that illustrates the problem.

@mkArtakMSFT I have create a github repo here: https://github.com/daping123/ModelBindingProj

When you post only ProductImage in postman,I will receive data count=0. When you post both ProductImage and IsDefaultImage,I will receive the correct data.

Why?Does IFormFile run the different pipeline in model binding?And my workaround is that,when I remove [ApiController] and [FromForm], I could pass ProductImage without IsDefaultImage.For my workaround,i am also confused,why i can’t add [FromForm]?

And I find if I receive Model instead of List<Model> in backend.Using [FromForm] is OK.

1reaction
coderealmcommented, Jan 10, 2021

I have spent the last 3 days looking to find how others have solved this problem but nothing out there works. Guess it is bug. When will this be released to production please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with model binding in file input type
I'm working on an Asp.Net Core MVC project. In my model, I have an IFormFile property. When I use a simple input tag...
Read more >
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Read more >
Upload files in ASP.NET Core
ASP.NET Core supports uploading one or more files using buffered model binding for smaller files and unbuffered streaming for larger files.
Read more >
File uploads in ASP.NET Core - About me and my site.
When uploading files using model binding and the IFormFile interface, ... The following error indicates your file upload exceeds the server's configured ......
Read more >
Upload Single Or Multiple Files In ASP.NET Core Using ...
In this article, we are going to see how to upload files in asp.net core web application and store in root directory of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found