我已经完成了使用表单将照片上传到 MVC(照片作为参数发送到 AJAX CALL)控制器的测试,现在需要做同样的事情,但需要修改:
有效的原始来源是:
<form action="/Home/Upload" method="post" enctype="multipart/form-data">
<label for="photo">Photo:</label>
<input type="file" name="photo" id="photo" />
<input id="uploadbtn" type="submit" value="Upload" />
</form>
和来自控制器的操作代码:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase photo)
{
// Verify that the user selected a file
if (photo != null && photo.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(photo.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/C:/poze"), fileName);
photo.SaveAs(path);
}
return Json(new { Success = true });
}
这有效,但在这种情况下不起作用:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="control-group">
<label class="control-label" for="photo">Photo:</label>
<div class="controls">
<input type="file" name="photo" id="photo" />
</div>
</div>
}
和动作代码是:
public ActionResult EditModule(INT ID, string Description, HttpPostedFileBase photo)
{
if (photo != null && photo.ContentLength > 0)
{
// extract only the fielname
var fileName = Name + ".png";
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/Content/Module Previews"), fileName);
photo.SaveAs(path);
}
else
{
Status += "Error image";
Success = false;
}
}
当我调试 EditModule 时,照片的值为 NULL。我确定 javascript 和 ajax 调用没有问题。