Skip to content

Instantly share code, notes, and snippets.

@speratus
Created January 20, 2020 14:54
Show Gist options
  • Save speratus/fbadf3a8df165bf42d2800962a11bef6 to your computer and use it in GitHub Desktop.
Save speratus/fbadf3a8df165bf42d2800962a11bef6 to your computer and use it in GitHub Desktop.
The form event listener to upload an image to a server in base64
document.querySelector("#form").addEventListener('submit', (e) => {
e.preventDefault()
const file = e.target['file'].files[0]
const reader = new FileReader();
reader.addEventListener('load', (e) => {
console.log(e)
console.log('The data is', e.target.result)
fetch("http://localhost:3000/images", {
method: "POST",
headers: {
"Content-Type": "application/json",
'Accept': 'application/json'
},
body: JSON.stringify({
"img": e.target.result
})
}).then(res => {
console.log(res.result)
})
})
const data = reader.readAsDataURL(file)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment