你的动作应该是这样的:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
取自:http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
然后使用jQuery Dialog进行文件上传:
$dialog.dialog("option", "buttons", {
"Save": function () {
var dlg = $(this);
var formData = new FormData($("#" + formName)[0]);
$.ajax({
url: /Controller/upload,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response, textStatus, xhr) {
...
}
},
error: function (xhr, status, error) {
....
}
});
},
"Cancel": function () {
$(this).dialog("close");
$(this).empty();
}
});