我想在asp.net mvc中使用ajax上传个人资料照片,但我不知道为什么上传参数开始无效。
HTML代码;
<div class="preview">
<div class="thumb-info mb-md">
<div id="preview">
<img src="" id="thumb" class="rounded img-responsive" alt="Profil Fotoğrafı" style="display: none;">
</div>
</div>
<div class="thumb-info-title">
<span class="thumb-info-inner">@ViewBag.CurrentUser</span>
<span class="thumb-info-type">CEO</span>
</div>
</div>
<form id="newHotnessForm" method="post" action="" enctype="multipart/form-data">
<div>
<input id="imgFile" type="file" name="file" />
<button id="btnUpload" type="button" class="btn btn-primary btn-xs">Save</button>
</div>
</form>
jQuery代码;
<script>
$(document).ready(function () {
$("#btnUpload").click(function () {
var formData = new FormData();
formData.append('file', $('#imgFile')[0].files[0]);
$.ajax({
url: "@Url.Action("UploadPhoto", "Manage")",
method: "POST",
data: formData,
contentType: false,
processData: false,
success: function (response) {
if (response != 0) {
$("#thumb").attr("src", response);
$("#thumb").show();
} else {
alert("File could not upload.");
}
},
});
});
});
</script>
开始行动
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase upload)
{
// after save path
return Json(new {success = path});
}
感谢您的帮助
答案 0 :(得分:0)
操作参数名称和FormData
中的键必须匹配
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase upload)
//should be 'upload' as well
var formData = new FormData();
formData.append('upload', $('#imgFile')[0].files[0]);