FileUtils.ts 837 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Manipulation des images
  3. */
  4. class FileUtils {
  5. /**
  6. * Returns a blob with the given data and the image filetype
  7. *
  8. * @param data
  9. * @param filetype
  10. */
  11. public static newBlob(data: string, filetype: string = 'image/jpeg'): Blob {
  12. return new Blob([data as BlobPart], {type: filetype});
  13. }
  14. /**
  15. * Transforme un Blob en Base64
  16. * @param {Blob} blob
  17. */
  18. public static async blobToBase64(blob: Blob): Promise<string> {
  19. return new Promise((resolve, _) => {
  20. const reader = new FileReader();
  21. // @ts-ignore
  22. reader.onloadend = () => {
  23. // @ts-ignore
  24. let base64Data: string = reader.result;
  25. base64Data = base64Data.substring(base64Data.indexOf(',') + 1);
  26. resolve(base64Data);
  27. };
  28. reader.readAsDataURL(blob);
  29. });
  30. }
  31. }
  32. export default FileUtils