const form = document.getElementById('form');
form.addEventListener('submit', function(event) {
event.preventDefault();
const uploadElement = document.getElementById('file'); // Select file input element
const file = uploadElement.files[0]; // Selects file at position 0 (for single file)
const payload = new FormData(); // Sending in FormData object will set headers
payload.append('CV', file, 'CV.pdf'); // Appends file to FormData object
fetch('https://httpbin.org/post', { // Endpoint is a test API
method: "POST", // or "PUT"
body: payload,
})
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log(err))
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter