使用jQuery Ajax发布时,Httppostedfilebase始终返回null

时间:2019-09-13 05:21:30

标签: c# jquery ajax asp.net-mvc asp.net-ajax

我正在开发mvc应用程序,并希望将文件上传到服务器。 在控制器触发事件​​时,fileupload为null。model.fileupload也具有路径。 实际上是使用ajax post方法提交我的表单。

cshtml代码。...

<form id="frmRegistrationDetails" method="post" enctype="multipart/form-data">
    <table>
      <tr>
         <td>
            <label>Upload File</label><input type="file" id="fileupload"  name="fileupload" />
         </td>
      </tr>
   </table>
</form>

控制器代码

 [HttpPost]
 public JsonResult SaveDetails(reg_up_mvc model,HttpPostedFileBase fileupload)
  {
  try
  {
    detailsRegistration registrationContext = new detailsRegistration();
    reg_up_mvc registrationDetails = new reg_up_mvc();
    registrationDetails.full_name = model.full_name;
    registrationDetails.fileupload = model.fileupload;
if (model.fileupload != null)
{
string sPath = Path.Combine(Server.MapPath("~/UploadedFilesFromRegistration"), fileupload.FileName);
fileupload.SaveAs(sPath);
registrationDetails.fileupload = sPath;
}

jquery

var data = {
        model: {
                fileupload: $('#fileupload').val()
               }
    };
     $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/Controller/ActionName",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
                           alert("successful")
                           }
         });

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/questions/28373894/httppostedfilebase-is-null-using-jquery-ajax?noredirect=1&lq=1

已在此onecheck中回答