【发布时间】:2020-01-14 23:29:51
【问题描述】:
我正在开发一个 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")
}
});
【问题讨论】:
-
请
console.log和JSON.stringify(data),告诉我们它是什么。 -
model:fileupload:"C\fakepath\download.jpg".forgot to add id to
-
曾尝试过使其相同但无能为力。所以只是更改了它的名称字段并尝试按名称元素检索值。但仍然没有希望
-
model:fileupload:"C\fakepath\download.jpg"这看起来不像 JSON。你确定正是吗? -
,"fileupload":"C:\\fakepath\\download-hill.jfif"那不是文件 contents。那是文件的路径。服务器对此无能为力。
标签: c# jquery ajax asp.net-mvc asp.net-ajax