Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Upload Image (HttpPostedFileBase is Null)

I tried to have an images uploader but when I click the browse and select image then submit my form the HttpPostedFileBase is null. Is there missing with my implementation? Please help.

View

<form action="Home/UploadImage" method="POST">
    <p>
        <input type="file" accept="image/x-png, image/gif, image/jpeg" name="projectBgImage" id="projectBgImage"/>
    </p>
    <input type="submit" value="submit"/>
</form>

Home Controller

 public ActionResult UploadImage(HttpPostedFileBase projectBgImage)
    {
        return View();
    }

Debug Image

enter image description here

like image 707
Jobert Enamno Avatar asked Aug 14 '26 03:08

Jobert Enamno


1 Answers

add enctype attribute your form element

<form action="Home/UploadImage" method="POST" enctype="multipart/form-data"> 

I suggest you to use BeginForm html-helper like this:

@using (Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { id = "form", enctype="multipart/form-data" }))
{
    <p>
        <input type="file" accept="image/x-png, image/gif, image/jpeg" name="projectBgImage" id="projectBgImage"/>
    </p>
    <input type="submit" value="submit"/>
}

and Action Method with [HttpPost] annotation

[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase projectBgImage)
{
    return View();
}

Extra Information : A nice blog post by Phil Haack Asp.net MVC Upload

like image 155
AliRıza Adıyahşi Avatar answered Aug 15 '26 16:08

AliRıza Adıyahşi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!