如何在mvc中使用ajax beginform传递已发布的文件?

时间:2017-03-04 11:15:58

标签: asp.net-mvc-4

我在部分视图中有以下内容。

@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()" />
}
我的控制器中的

是方法。

[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);
}

从上面我应该得到我的控制器上发布的文件,但它没有给我文件,而是在控制器操作上给出null。

请建议。

0 个答案:

没有答案
相关问题