0

HTTPPostedFileBase is always null in the controller. Please review and let me know where am I wrong.

Controller Post method

 public ActionResult EditProfile(Contact model, HttpPostedFileBase picture, string currentPassword, string CurrentPasswordQ, string newPassword, string loginPwd, string currentPinQ, string newPin, int? selectedQuestion, string answer, bool pwdChange = false, bool questionChange = false, bool pinChange = false)
    {

My Form Header

@using (Html.BeginForm("EditProfile", "CompanyAdmin", FormMethod.Post, new { enctype = "multipart/form-data", @data_ajax = "false" }))

{

And my file Input

 <tr>
                                                <td class="label_form_div">
                                                    <label>Profile Picture</label>
                                                </td>
                                                <td>
                                                    <input type="file" name="picture" />
                                                </td>
                                            </tr>

Please review and see if you can find what is wrong with this. Thanks

Waheed Asghar
  • 159
  • 1
  • 9

2 Answers2

0

Its seems your code is correct, anyway try this way in the controller. It might helpful for you:

 public ActionResult EditProfile(Contact model, string currentPassword, string CurrentPasswordQ, string newPassword, string loginPwd, string currentPinQ, string newPin, int? selectedQuestion, string answer, bool pwdChange = false, bool questionChange = false, bool pinChange = false)
    {

if (Request.Files != null && Request.Files.Count > 0)
{
    HttpPostedFileBase file = Request.Files[0];
    if (file != null && file.ContentLength > 0)
    {
             //other logic
    }
}

}

Source: https://stackoverflow.com/a/32219011/3397630

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
0

Please follow the below steps.

Step :- 1

@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, 
                            new { enctype = "multipart/form-data" }))
{  
    <label for="file">Upload Image:</label> 
    <input type="file" name="file" id="file" style="width: 100%;" /> 
    <input type="submit" value="Upload" class="submit" /> 
}

Step :- 2

 public ActionResult FileUpload(HttpPostedFileBase file)
    {
        if (file != null)
        {
            string pic = System.IO.Path.GetFileName(file.FileName);
            string path = System.IO.Path.Combine(
                                   Server.MapPath("~/images/profile"), pic); 
            // file is uploaded
            file.SaveAs(path);

            // save the image path path to the database or you can send image 
            // directly to database
            // in-case if you want to store byte[] ie. for DB
            using (MemoryStream ms = new MemoryStream()) 
            {
                 file.InputStream.CopyTo(ms);
                 byte[] array = ms.GetBuffer();
            }

        }
    // after successfully uploading redirect the user
    return RedirectToAction("actionname", "controller name");
}
Shahzad Khan
  • 432
  • 2
  • 14