Asp.net mvc 4 如何在mvc中使用ajax传递发布的文件?
我有以下我的部分看法Asp.net mvc 4 如何在mvc中使用ajax传递发布的文件?,asp.net-mvc-4,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" val
@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()" />
}
从上面我应该得到我的控制器上发布的文件,但它不给我文件,而是给控制器上的null操作
请建议。您不能使用
Ajax.BeginForm()
上传文件。如果要使用ajax上传文件,请使用$.ajax
方法和FormData
。感谢您的宝贵支持。我使用了不在Ajax.BeginForm中的部分视图来实现这一点。
[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);
}