asp.net-mvcfile-uploadasp.net-mvc-5httppostedfilebase

asp.net mvc-5 HttpPostedFileBase is null


i am messing around with file uploading using asp.net mvc-5 HttpPostedFileBase but it is showing me HttpPostedFileBase is null after i am selecting the image

here is my code

 <input type="file" title="search image" file-model="profileimage" id="allfilepath" name="file" />
 <button type="submit" class="btn btn-primary col-sm-5">
         <span class="glyphicon glyphicon-plus"></span>
 </button>

and my controller

    [HttpPost]
    public ActionResult insert(HttpPostedFileBase file, quotationcompany quotecomp)
    {
        var allowedExtensions = new[] {
        ".Jpg", ".png", ".jpg", "jpeg"
    };
        if (file == null)
        {
            ViewBag.message = "Please Select Image";
            return View();
        }
        else {
            ViewBag.message = "Image Uploaded Successfully";
            return View();
        }

    }

if (file == null)(on this line (file) is showing me null after i am uploading png image)

what is wrong in this code?


Solution

  • Check Form Attributes

    A common mistake people make is missing the following part in the form tag:

    <form enctype="multipart/form-data">
    </form>
    

    Also in MVC your form structure could look something like this

    @using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data", @id = "myForm", @role="form" }))