0

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?

  • An advice! Please use [Search](https://stackoverflow.com/search?q=HttpPostedFileBase+is+null) before posting duplicate questions otherwise your question can get down votes (*As you already has 9 questions*) and this might cause you [Question Ban](https://stackoverflow.com/help/question-bans) – mmushtaq May 23 '17 at 07:25

1 Answers1

7

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" }))
Community
  • 1
  • 1
Terrance00
  • 1,388
  • 1
  • 18
  • 27