Created
January 20, 2020 14:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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