IFormFile is always null when receiving file from console app

假如想象 提交于 2019-12-04 18:42:14

Finally I made it Working. It must be a bug in microsoft or they may have change the way things works from .net core 2.1 for ApiController.

I changed my WebApi method to the following and voila it worked.

Note: removed ApiController and it worked. Microsoft should document this.

[Route("api/[controller]")]
public class OCRController : ControllerBase
 {
    [HttpPost]
    public async Task<IActionResult> Post([FromForm] IFormFile file)
    {
    }
}

It may help someone who is struggling like me.

The answer to this question still stands but for anyone who wants to retain the [ApiController] attribute there is a workaround as described in this GitHub thread.

Basically, try to expand the [FromForm] attribute like so...

[Route("api/[controller]")]
public class OCRController : ControllerBase
 {
    [HttpPost]
    public async Task<IActionResult> Post([FromForm(Name = "file")] IFormFile file)
    {
    }
}

try explicit content type to your MultipartFormDataContent

var contentfile = new StreamContent(fs);
contentfile.Headers.ContentType = MimeMapping.GetMimeMapping(fileName);
content.Add(contentfile, "file", fn);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!