我正在尝试上传多个文件以及普通表单数据。我以前用 PHP 实现的这个东西现在我用 C# 的 ASP.NET MVC4 来做这个。我有一个 html 表单
<form action="/controller/actionane" name="applicationform" class="upload-form" method="post" onsubmit="return false;" enctype="multipart/form-data" id="userform">
<input class="form-control" type="file" class="upload-file" data-max-size="12582912" multiple="multiple" name="attachment[]" value="documents">
<input class="btn btn-success" type="submit" name="submit" onclick="formSubmit()" />
</form>
我的 jquery-1.11.1 Javascript 代码如下所示:
function formSubmit() {
var form = $("form[name='applicationform']");
var data = new FormData(form[0]);
$.ajax(
{
method: "POST",
url: form.attr("action"),
processData: false, // Don't process the files
contentType: false, cache: false, async: false,
data: data,
success: function (data) {
alert(data);
}
});
}
我的控制器看起来像这样
[HttpPost]
public JsonResult submitApplication(HttpPostedFileBase[] attachment)
{
string fil= "";
foreach (HttpPostedFileBase file in attachment)
{
/*Geting the file name*/
string filename = System.IO.Path.GetFileName(file.FileName);
fil += filename;
/*Saving the file in server folder*/
file.SaveAs(Server.MapPath("~/Images/" + filename));
string filepathtosave = "Images/" + filename;
/*HERE WILL BE YOUR CODE TO SAVE THE FILE DETAIL IN DATA BASE*/
}
return this.Json(fil,JsonRequestBehavior.AllowGet);
}
但这不是将文件传递给参数对象引用抛出空异常我应该怎么做才能让它运行?