1function getBase64(file) {
2 return new Promise((resolve, reject) => {
3 const reader = new FileReader();
4 reader.readAsDataURL(file);
5 reader.onload = () => resolve(reader.result);
6 reader.onerror = error => reject(error);
7 });
8}
9
10var file = document.querySelector('#files > input[type="file"]').files[0];
11getBase64(file).then(
12 data => console.log(data)
13);
1var fileInput = document.getElementById('#myInputID');
2
3var reader = new FileReader();
4reader.readAsDataURL(fileInput.files[0]);
5
6reader.onload = function () {
7 console.log(reader.result);//base64encoded string
8};
9reader.onerror = function (error) {
10 console.log('Error: ', error);
11};
12