Input file as append in FormData

0

How can I insert images as append into a FormData ?

I'm trying this way, but when I use console.log to check the dados variable, imagens is empty.

var dados = new FormData();

$('.copiar-elemento').each(function() {
   $(this).find('input:file').each(function(index, element) {
         dados.append('cores[imagens][]', element.files[0]);
    });
});
    
asked by anonymous 08.04.2018 / 23:46

1 answer

0

Try this:

var data = new FormData();

$.each($("input[type='file']")[0].files, function(i, file) {
    data.append('cores[imagens][]', file);
});

$.ajax({
    type: 'POST',
    url: '/sua/url',
    cache: false,
    contentType: false,
    processData: false,
    data : data,
    success: function(result){
        console.log(result);
    },
    error: function(err){
        console.log(err);
    }
})
    
09.04.2018 / 15:50