0

I have following in my partial view.

@using (Ajax.BeginForm("xyz", "xyz", new AjaxOptions { HttpMethod = "POST"     }, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="FileName" id="FileName" style="width:240px" />
    <input type="submit" value="Upload" onclick="submit()" />
}

in my controller following is method.

[HttpPost]
//[ValidateAntiForgeryToken]
public JsonResult xyz(HttpPostedFileBase FileName)
{
    var httpPostedFileBase = Request.Files["FileName"];
    if (httpPostedFileBase != null && httpPostedFileBase.ContentLength > 0)
    {
        string extension = System.IO.Path.GetExtension(httpPostedFileBase.FileName);
        string path1 = string.Format("{0}/{1}", Server.MapPath("~/SavedFiles"), extension);
        if (System.IO.File.Exists(path1))
            System.IO.File.Delete(path1);

        httpPostedFileBase.SaveAs(path1);
    }
    ViewData["Status"] = "Success";
    return Json("test", JsonRequestBehavior.AllowGet);
}

From the above i should get the file on my controller that is posted but it does not give me file instead gives null on controller action.

Please suggest.

  • You cannot upload files using `Ajax.BeginForm()`. If you want to upload files using ajax, then use the `$.ajax` methods and `FormData`. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Mar 04 '17 at 22:58
  • thanks for your valuable support. I have achieved this using partial view which is not in Ajax.BeginForm. – Nimit Parmar Mar 06 '17 at 12:43

0 Answers0