-2

My file upload is not working. I want to upload images. I have already created the input type file in my view. Here is my code-

[HttpPost]
public ActionResult AddOrEdit(tbl_employee emp)
{
    var getGender = db.tbl_gender.ToList();
    SelectList list = new SelectList(getGender, "gender_type", "gender_type", emp);
    ViewBag.genderList = list;

    if (emp.employee_image != null)
    {
        string fileName = Path.GetFileNameWithoutExtension(emp.imageFile.FileName);
        string extension = Path.GetExtension(emp.imageFile.FileName);
        fileName = fileName + extension;
        emp.employee_image = "~/Images/Employee/" + fileName;
        fileName = Path.Combine(Server.MapPath("~/Images/Employee/"), fileName);
        emp.imageFile.SaveAs(fileName); 
    }
    if (emp.employee_id == 0)
    {
        db.tbl_employee.Add(emp);
        db.SaveChanges();
        return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
    }
    else
    {
        db.Entry(emp).State = EntityState.Modified;
        db.SaveChanges();
        return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
    }     
}

Here is my tbl_employee.cs

public long employee_id { get; set; }
public string employee_image { get; set; }
public HttpPostedFileBase imageFile { get; set; }

What is wrong here?

1 Answers1

0

Include HttpPostedFileBase in your function for getting the uploaded file in your AddOrEdit function.

public ActionResult AddOrEdit(tbl_employee emp, HttpPostedFileBase Image)
{

}

For more explanation, include also the view with your question. Ensure your view contain code for image upload.

 @using (Html.BeginForm("AddOrEdit", "Employee", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
Alsamil Mehboob
  • 384
  • 2
  • 6
  • 24
  • I want to save the imageFile location in my database and retrieve the image in the index view. I am using jquery and ajax for POST method. I think that's the reason my image is not uploading via normal way. – Salman Zahir Aug 11 '17 at 06:27
  • Do you get value of uploaded file in controller? – Alsamil Mehboob Aug 11 '17 at 06:29