Javascript 使用ajax序列化时无法附加文件
我有一个联系我们的表单,它工作得很好,但是现在,为了阻止页面重新加载,我不得不使用ajax。表单正在序列化,但现在我无法添加任何附件文件 下面给出了我正在使用的代码。任何帮助都将不胜感激Javascript 使用ajax序列化时无法附加文件,javascript,jquery,ajax,asp.net-mvc,asp.net-ajax,Javascript,Jquery,Ajax,Asp.net Mvc,Asp.net Ajax,我有一个联系我们的表单,它工作得很好,但是现在,为了阻止页面重新加载,我不得不使用ajax。表单正在序列化,但现在我无法添加任何附件文件 下面给出了我正在使用的代码。任何帮助都将不胜感激 <div id="respond" class="comment-respond"> @model Umbraco.AddingValues.Web.Model.ContactUs @using (Html.BeginUmbracoForm("Contact", "ContactUsSurfac
<div id="respond" class="comment-respond">
@model Umbraco.AddingValues.Web.Model.ContactUs
@using (Html.BeginUmbracoForm("Contact", "ContactUsSurface", FormMethod.Post, new { @class = "contact-form", @id ="myForm", @enctype="multipart/form-data"} ))
{
@Html.ValidationSummary(true)
<div id="form">
<div class="comment-form-author">
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div class="comment-form-author">
@Html.LabelFor(x => x.Company)
@Html.TextBoxFor(x => x.Company)
@Html.ValidationMessageFor(x => x.Company)
</div>
<div class="comment-form-email">
@Html.LabelFor(x => x.EmailFrom, "Email")
@Html.TextBoxFor(x => x.EmailFrom)
@Html.ValidationMessageFor(x => x.EmailFrom,"Please enter a valid Email Address")
</div>
<div class="comment-form-author">
@Html.LabelFor(x => x.Tel)
@Html.TextBoxFor(x => x.Tel)
@Html.ValidationMessageFor(x => x.Tel)
</div>
<div class="comment-form-url">
@Html.LabelFor(x => x.Intrested)
@Html.TextBoxFor(x => x.Intrested)
@Html.ValidationMessageFor(x => x.Intrested)
</div>
<div class="comment-form-comment" >
@Html.LabelFor(x => x.Message)
@Html.TextAreaFor(x => x.Message)
@Html.ValidationMessageFor(x => x.Message)
</div>
</div>
<div class="forms-button" id="submitbutton">
<input id="submit" class="button" name="submit" type="button" value="submit" onclick="submitSearchForm_ajax('myForm', 'message')"/>
<div class="fileUpload ">
@* <input type="file" class="upload" />*@
<input type="file" name="attachment" id="attachment" class="upload" value ="Upload" />
</div>
<div id="message" style="display:none">
<br />
<div style="margin-top:10px">
<h2>Thank You! Your message has been recieved.</h2>
</div>
</div>
</div>
}
</div>
表单序列化时不包含文件,在使用ajax发布时,您必须使用formData对象来包含文件。您可以用一个示例说明我是新手吗?您可以使用AjaxUploader使用ajax上载文件这可能对您有所帮助:
function submitSearchForm_ajax(formid, updateDivid) {
var form = $("#" + formid);
//var formData = new FormData($('myForm')[0]);
$('#message').css('display', 'none');
//var formData = new FormData($('#myForm')[0]);
$.ajax({
url: form.attr("action"),
//data: form.serialize() ,
data: form.serialize(),
type: 'POST',
success: function (message) {
$('#message').css('display', 'block');
if (message == "Failed") {
$('#message').html('Error submitting form.');
}
else {
$('#message').html(message);
$("#form :input[type='text']").val('');
}
}
})
}