Uploading image with JS

1

Good afternoon, I'm trying to upload image using JS but I'm not getting it, I'm not using jQuery, I'm using Axios.

So far, I've tried it that way

var files = document.getElementById("inputPhoto").files;

axios.post(url+'api.php', {
        file: new FormData(imagem])
    }, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
    .then(function(response){
        console.log(response)
    })
    .catch(function(error){
        console.log(error)
    })

NOTE: I am using the HTML5 multiple, BackEnd is already ready in PHP

    
asked by anonymous 18.06.2017 / 17:09

1 answer

2

According to Repository Suggestions on Github , you can simply add to FormData :

var files = document.getElementById("inputPhoto").files;
let data = new FormData();

for (var i = 0, l = files.length; i < l; i++) {
  let file = files.item(i);
  data.append('images[' + i + ']', file, file.name);
}

const config = {
  headers: {
    'content-type': 'multipart/form-data'
  }
}

axios.post('/api/images', data, config);
    
18.06.2017 / 17:56