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);
1// Convert file to base64 string
2export const fileToBase64 = (filename, filepath) => {
3return new Promise(resolve => {
4var file = new File([filename], filepath);
5 var reader = new FileReader(); // Read file content on file loaded event
6 reader.onload = function(event) {
7 resolve(event.target.result); }; // Convert data to base64
8 reader.readAsDataURL(file);
9});
10};
11// Example call:
12fileToBase64("test.pdf", "../files/test.pdf").then(result => { console.log(result);});