MVC 및 jQuery에서 파일 업로드를 수행하려고하지만 내 paremter가 null입니다. 내 jQuery에서 양식 데이터를 가져 와서 변수에 할당 한 다음 ActionResult에 게시합니다. 내 매개 변수가 null이고 데이터를 올바르게 전달하는지 잘 모르겠습니다.POST 이미지 파일 - ActionResult의 매개 변수가 null입니다.
HTML
<table>
<tr>
<td><img src="@Url.Content("~/AMTImages/UserAvatar.png")" width="64" height="64" style="border: thin solid" /></td>
<td></td>
<td></td>
<td>
<form enctype="multipart/form-data" id="imageUploadForm">
<table>
<tr>
<td>@Html.LabelFor(m => m.DisplayName, "Display Name: ")</td>
<td style="padding-left: 10px;">@Html.TextBoxFor(m => m.DisplayName)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.UserImage, "User Image: ")</td>
<td style="padding-left: 10px; float: right;">@Html.TextBoxFor(m => m.UserImage, new { type = "file", accept = "image/*" })</td>
</tr>
</table>
</form>
</td>
<td>
<table style="display:none;">
<tr>
<td>
<progress></progress>
</td>
</tr>
</table>
</td>
</tr>
</table>
자바 스크립트
$(':file').change(function() {
var file = this.files[0];
$.ajax({
url: '@Url.Action("UploadImage", "User")', //Server script to process data
type: 'POST',
data: file,
beforeSend: function() {
},
success: function() {
},
error: function() {
},
processData: false,
contentType: file.type
});
});
ActionResult는는
public ActionResult UploadImage(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Images"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return View();
}
@Rory McCrossan - 내 게시물은 ASP.NET MVC에 관한하고 그 답은 클라이언트 코드를 보여줍니다. –
맞습니다. 여기에있는 문제는 클라이언트 쪽 코드로 인해서 내가 솔루션에 연결된 질문과 동일합니다. –