📜  以角度将图像转换为 base64 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:23.908000             🧑  作者: Mango

代码示例1
// works like charm in pdfMake and angular
//You can use this function to create generate a base64 image

        toDataURL = async (url) => {
        console.log("Downloading image...");
        var res = await fetch(url);
        var blob = await res.blob();
    
        const result = await new Promise((resolve, reject) => {
          var reader = new FileReader();
          reader.addEventListener("load", function () {
            resolve(reader.result);
          }, false);
    
          reader.onerror = () => {
            return reject(this);
          };
          reader.readAsDataURL(blob);
        })
    
        return result
      };

// and then call it like this

    imageSrcString = await this.toDataURL(imageSrc)