我使用Ajax.Beginform menthod上传文件,但它不起作用,请帮帮我。
文件Create.cshtml:
@using (Ajax.BeginForm("UpLoadFile", "KG", new AjaxOptions { HttpMethod = "POST" }, new { enctype = "multipart/form-data", @id = "form-uploadfile" }))
{
<input type="file" name="fileupload" id="fileupload" />
<input type="submit" value="UpLoad" />
}
服务器端的控制器
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase fileupload)
{
if (fileupload != null)
{
// Verify that the user selected a file
if (fileupload != null && fileupload.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(fileupload.FileName);
// TODO: need to define destination
var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
fileupload.SaveAs(path);
}
}
return Json(new { success = true });
}
参数fileupload alway null,我引用了很多帖子但仍然没有解决问题:(
抱歉,我的英语非常糟糕