0

I have an application where i use an upload component. I want to convert the image in base64 and to get this data. This is why i created:

 getBase64(image, callback) {
    const reader = new FileReader();
    reader.addEventListener("load", () => callback(reader.result));
    console.log(reader.result);
    reader.readAsDataURL(image);
  }

The issue is that i can't get this format for the uploaded image. How to solve this?

demo: https://codesandbox.io/s/complete-control-over-file-list-ant-design-demo-tll41?file=/index.js:246-449

Asking
  • 3,487
  • 11
  • 51
  • 106
  • The normal solution is to use a canvas, that has a built in `toDataURL()` [method](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL). This question isn't React-specific, but the principle still applies: https://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript – DBS Apr 22 '20 at 14:22

1 Answers1

0

I'd use HTML5 <canvas> and toDataURL API https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL

01000010
  • 64
  • 5