$(function(){
$('#my_form').on('submit', function(e){
e.preventDefault();
var $that = $(this),
formData = new FormData($that.get(0));
formData.append('date_upl', new Date()); // добавляем данные, не относящиеся к форме. У нас - это дата
$.ajax({
url: $that.attr('action'),
type: $that.attr('method'),
contentType: false,
processData: false,
data: formData,
dataType: 'json',
success: function(json){
if(json){
$that.replaceWith(json);
}
}
});
});
var uploadButton = $('<button/>')
.addClass('btn btn-primary')
.text('Processing...')
.on('click', function () {
$('#my_form').submit();
}) ;
function tstFile(val){
var reg =new RegExp("^text/xml|x-json");
var tst = val.match(reg);
if (tst){
return true;
}
return false;
}
$('#fileupload').change(function(e){
var file=e.currentTarget.value
var type=e.currentTarget.files[0].type
var type_text=type==='text/xml'?'to JSON':'to XML'
if(tstFile(type)){
uploadButton.text(type_text)
var node = $('<p/>')
.append($('<span class=""/>').text(file))
.append(uploadButton.clone(true));
node.appendTo('#table_btn');
}
})
});