Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my file always show null when attempting to upload

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading