我正在开发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")
}
});
答案 0 :(得分:0)
https://stackoverflow.com/questions/28373894/httppostedfilebase-is-null-using-jquery-ajax?noredirect=1&lq=1
已在此onecheck中回答