Search code examples
csvpost.net-corepostmaniformfile

How can .NET Core's IFormFile recognise a .csv file?


I'm trying to post a .csv file using Postman, but when I debug it always has a null value.

I checked the csv file and it should be valid, I can open it both with Microsoft Office and Open Office.

I sent it both as form-data and as a binary in Postman, but neither worked.

I tried adding a [FromForm] attribute, like in this answer: https://stackoverflow.com/a/54570203/1984657

I also tried as IFormFileCollection, but this didn't work either.

Below is the stripped down code, even here it still receives a null value

namespace csvApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CsvController : ControllerBase
    {
        [HttpPost]
        public ActionResult Post(IFormFile csv)
        {
            return Ok(csv);
        }
    }
}

What am I missing?


Solution

  • It works when instead of passing it as parameter to my Post method, I'm getting it from Request.Form.Files:

    [HttpPost]
    public ActionResult Post()
    {
        var file = Request.Form.Files[0];
        return Ok(file);
    }