Get base64 from File Response.

    0

    3

    Nilberto RodrΓ­guez Rojas

    This file will be loaded as a text file. The code first creates a new Blob object and sets the properties to indicate that the file is an PDF file. It then saves the base64 encoded version of the blob to a new variable called b64. The final step is to use the setBase64 function to set the b64 variable to the base64 encoded blob.

    Library: react

    // const myblob = new Blob([data], {
    //   type: 'application/pdf',
    // });
    // const b64 = await getBase64(myblob);
    // setBase64(b64);
    
    const getBase64 = (file: Blob) => {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
          resolve(reader.result);
        };
        reader.onerror = function (error) {
          console.log('Error: ', error);
          reject(error);
        };
      });
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.