use fetch api with file code example
Example 1: fetch api javascript
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
});
Example 2: uploading file with fetch
const input = document.getElementById('fileinput');
const upload = (file) => {
fetch('http://www.example.net', {
method: 'POST',
headers: {
"Content-Type": "You will perhaps need to define a content-type here"
},
body: file
}).then(
response => response.json()
).then(
success => console.log(success)
).catch(
error => console.log(error)
);
};
const onSelectFile = () => upload(input.files[0]);
input.addEventListener('change', onSelectFile, false);