I want to upload a file. The problem I have is the parameter is always null
My HTML
<form action="Admin/UploadImage/" method="post" enctype="multipart/form-data">
<input type="file" id="file">
<input type="submit">
</form>
and my controller is
[HttpPost]
[Route("UploadImage")]
public async Task<string> UploadImage([FromForm] IFormFile file)
{
//file is always null...
return "hi";
}
I have tried ([FromBody] IFormFile file) and (IFormFile file)
I load the web page and select a 50kb jpg. I then click submit (there is nothing unexpected in how I’m using this)
Why does it always show null
>Solution :
You are missing the name attribute in the <input> element.
You should add the following line :
<input type="file" id="file" name="file">