首页 > MVC-前台调用后台action 传递upload file 参数问题

MVC-前台调用后台action 传递upload file 参数问题

 

在后台获取upload file 数量的时候发现count一直为0,经检查发现了问题 ,代码如下:

 

前台:

var data = $("#DetailForm").serialize();

$.ajax({

url: '@Url.Action("SaveRequest", "RegistrationRequest")',

type: "POST",

dataType: "JSON",

data: data,

cache: false,

processData: false,

success: function (result) {

window.scrollTo(0, 0);

utility.hideMask();

FormSaveSuccess(result);

}

});

 

后台:

[HttpPost]

public ActionResult SaveRequest(DetailModel model)

{

   if (ModelState.IsValid)

   {

     if (model.Type == Codes.VT.ToString() || model.Type == Codes.NT.ToString())

     {

     model.DocumentsList = Utility.BuildDocumentModel(Request.Files, model.DocumentSearchCriteria.ModuleCode,  "", "",  model.DocumentSearchCriteria.FunctionId);

     if (model.DocumentsList.Count == 0)

    {

       return Json(new { HasError = true, ErrorMessage = MessageResources._NoUploadFile });

    }

  }

}

}

Request.Files 里文件count ==0.

解决这个问题只需要在前台调用action脚本里加上 属性 mimeType: "mutipart/form-data",

 

$.ajax({

url: '@Url.Action("SaveRequest", "RegistrationRequest")',

type: "POST",

dataType: "JSON",

data: new FormData(this),

mimeType: "mutipart/form-data",

cache: false,

processData: false,

success: function (result) {

window.scrollTo(0, 0);

utility.hideMask();

FormSaveSuccess(result);

}

});

转载于:https://www.cnblogs.com/wwzhang/p/4944860.html

更多相关: