1

有一个处理文件上传的控制器方法:

AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(int id, HttpPostedFileBase uploadFile)
{
    if (uploadFile != null && uploadFile.ContentLength > 0 && ...)
    ...
}

当我使用 Html.BeginForm 时,一切正常。将 Html.BeginForm 替换为 Ajax.BeginForm 会导致 uploadFile 的值为空(上述方法的第二个参数):

<div id="ajaxDocumentUpload">
@{ using (Ajax.BeginForm("FileUpload", "ProjectDocument", FormMethod.Post, 
           new AjaxOptions { OnSuccess = "UploadSuccess" }, 
           new { enctype = "multipart/form-data" }))
  {
    <div>
        <input type=file accept="image/gif, image/jpeg" name="uploadFile">     
    </div>
    <div>
        <input type="hidden" name="id" id="id" value="@Model.ProjectId"/>
    </div>

    <input id=btnUpdateAttachment type=submit value="Upload">
  } 
}
</div>

知道有什么问题吗?提前感谢您的帮助!

4

1 回答 1

0

您将不兼容的参数传递给Ajax.BeginForm(). 改用这个:

@using(Ajax.BeginForm("FileUpload", "ProjectDocument", null, 
    new AjaxOptions { OnSuccess = "UploadSuccess" }, 
    new { enctype = "multipart/form-data" })

MSDN 文档

于 2013-10-09T09:20:21.000 回答