Uploading file using ajax

27.09.20

Let's upload a file to the server using ajax. Let's use axios.

let formData = new FormData(),
    input = document.getElementById('file');

//"file" - key on server
formData.append("file", input.files[0]);  

// handler/path - your path
axios.post('/handler/path', formData, { 
   headers: {
     'Content-Type': 'multipart/form-data'
    }
}).then(res => {
   console.log('success')
}).catch(e =>{
   console.log('error')
})
blog comments powered by Disqus