I need to send a file from a form as a POST method of jquery, I'm doing so, but it does not work, what can I be wrong?
$('#file-upload').change(function (event) {
form = new FormData();
form.append('file-upload', event.target.files[0]); // para apenas 1 arquivo
//var name = event.target.files[0].content.name; // para capturar o nome do arquivo com sua extenção
});
Jquery integer
$(document).ready(function () {
$('.castrar_email_fom').validate({ // initialize the plugin
rules: {
email: {
required: true,
email: true
},
nome: {
required: true,
minlength: 5
}
},
messages: {
required: "Campo obrigatório",
remote: "Please fix this field.",
email: "Por favor insira um email válido",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
equalTo: "Please enter the same value again.",
maxlength: $.validator.format( "Não insira mais do que {0} caracteres." ),
minlength: $.validator.format( "Digite pelo menos {0} caracteres." ),
rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ),
range: $.validator.format( "Please enter a value between {0} and {1}." ),
max: $.validator.format( "Please enter a value less than or equal to {0}." ),
min: $.validator.format( "Please enter a value greater than or equal to {0}." ),
step: $.validator.format( "Please enter a multiple of {0}." )
},
submitHandler: function (form) { // for demo
$(".resultado_promocoes").html('<div class="spinner"></div>');
$('#file-upload').change(function (event) {
form = new FormData();
form.append('file-upload', event.target.files[0]); // para apenas 1 arquivo
//var name = event.target.files[0].content.name; // para capturar o nome do arquivo com sua extenção
});
var form = $('.castrar_email_fom');
$.ajax({
url: 'http://xx.com.br/email_promocoes.php',
type: 'POST',
data: form.serialize()
})
.done(function(data){
$('.resultado_promocoes').fadeOut('slow', function(){
$('.resultado_promocoes').fadeIn('slow').html(data);
});
})
.fail(function(){
alert('Ajax Submit Failed ...');
});
return false; // for demo
}
});
});
HTML Form
<form action="" method="post" class="contato_form" novalidate="novalidate">
<div cla="row">
<div class="col-md-6 formulario" >
<input type="text" name="nome" value="" size="40" class="" aria-required="true" aria-invalid="false" placeholder="Nome" style="
width: 96%;
">
</div>
<div class="col-md-6 formulario">
<input type="text" name="telefone" value="" size="40" class="" aria-required="true" aria-invalid="false" placeholder="Telefone">
</div>
</div>
<div cla="row">
<div class="col-md-12 formulario">
<input type="text" name="email" value="" size="40" class="" aria-required="true" aria-invalid="false" placeholder="Email">
</div>
</div>
<div cla="row">
<div class="col-md-12 formulario">
<textarea name="mensagem" cols="40" rows="10" class="" id="" aria-invalid="false" placeholder="Mensagem"></textarea>
</div>
</div>
<div cla="row">
<div class="col-md-6 center">
<label for="file-upload" class="custom-file-upload"><p></p><div class="botao_anexar_form botao_form vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-square vc_btn3-style-modern vc_btn3-color-grey"><i style="color: #4a0743 !important; font-weight: lighter;font-size: 19px;" class="fa fa-paperclip"></i> Anexar Arquivo</div><p><br> <span class="wpcf7-form-control-wrap file-612"><input type="file" name="file-upload" size="40" class="wpcf7-form-control wpcf7-file" id="file-upload" aria-invalid="false"></span></p></label></div>
<div class="col-md-6 center">
<button type="submit" class="botao_enviar_form vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-square vc_btn3-style-modern vc_btn3-color-grey">enviar</button>
</div>
</div>
<div cla="row">
<div class="col-md-12 center resultado_contato_fom">
</div>
</div>
</form>