converting an image to base64 in angular

Solutions on MaxInterview for converting an image to base64 in angular by the best coders in the world

showing results for - "converting an image to base64 in angular"
Diego Alejandro
24 May 2017
1// works like charm in pdfMake and angular
2//You can use this function to create generate a base64 image
3
4        toDataURL = async (url) => {
5        console.log("Downloading image...");
6        var res = await fetch(url);
7        var blob = await res.blob();
8    
9        const result = await new Promise((resolve, reject) => {
10          var reader = new FileReader();
11          reader.addEventListener("load", function () {
12            resolve(reader.result);
13          }, false);
14    
15          reader.onerror = () => {
16            return reject(this);
17          };
18          reader.readAsDataURL(blob);
19        })
20    
21        return result
22      };
23
24// and then call it like this
25
26    imageSrcString = await this.toDataURL(imageSrc)