Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
How I can get base64 string of image from FileReader

I am using below 2 methods but I am unable to get back base64 string from it.

function convertFileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = reject;
  });
}

function previewProductImages(files){
  let preveiwImagesTemplate = [];
  for(let i=0; i<files.length; i++ ){
      const uploadedImageBase64 = convertFileToBase64(files[i]);
      /// WANT BASE64 HERE So I can pass that to another method
  }
}

Replacement or better approches are welcome.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should first assign a handler to the onload property - and then call the readAsDataURL() method, not the opposite.

    function readFile(file)
      {
        return new Promise((resolve) =>
        {
          if (file.size)
          {
            const reader = new FileReader();
            reader.onload = (e) =>
            {
              resolve({
                binary: file,
                b64: e.target.result,
              });
            };
            reader.readAsDataURL(file);
          }
        });
      }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!