它上传图片但重定向到UploadImage.cshtml。显然ev.preventDefault不起作用。是否有解决方案,没有让我通过blueimp或xhtml的东西因为我还没准备好接触那些东西。
HTML
<form method="post" action="UploadImage.cshtml" id="imgUploadForm" class="imgUpload" enctype="multipart/form-data">
@FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag: false,
uploadText:"Upload")
<input type="submit" name="buttonUpload2" value="Upload" />
</form>
Jquery的
$(function () {
$('.imgUpload').submit(function (ev) {
var frm = $(this);
Upload(frm);
ev.preventDefault();
});
});
function Upload(frm) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: new FormData(frm),
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
}
答案 0 :(得分:0)
使用以下Jquery代码解决。感谢
$(document).ready(function (e) {
$('.imgUpload').on('submit', (function (e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
alert('success');
},
error: function (data) {
alert('error');
}
});
}));
})