1var xhr = new XMLHttpRequest();
2 xhr.open("GET", "/path/to/local/image/file", true);
3 xhr.responseType = "blob";
4 xhr.onload = function (e) {
5 console.log(this.response);
6 var reader = new FileReader();
7 reader.onload = function(event) {
8 var res = event.target.result;
9 console.log(res)
10 }
11 var file = this.response;
12 reader.readAsDataURL(file)
13 };
14 xhr.send()
1function getBase64Image(src, callback, outputFormat) {
2 const img = new Image();
3 img.crossOrigin = 'Anonymous';
4 img.onload = () => {
5 const canvas = document.createElement('canvas');
6 const ctx = canvas.getContext('2d');
7 let dataURL;
8 canvas.height = img.naturalHeight;
9 canvas.width = img.naturalWidth;
10 ctx.drawImage(img, 0, 0);
11 dataURL = canvas.toDataURL(outputFormat);
12 callback(dataURL);
13 };
14
15 img.src = src;
16 if (img.complete || img.complete === undefined) {
17 img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
18 img.src = src;
19 }
20 }
1function encodeImageFileAsURL(element) {
2 var file = element.files[0];
3 var reader = new FileReader();
4 reader.onloadend = function() {
5 console.log('RESULT', reader.result)
6 }
7 reader.readAsDataURL(file);
8}
9<input type="file" onchange="encodeImageFileAsURL(this)" />