Skip to content

Instantly share code, notes, and snippets.

@rafaeldcastro
Created May 24, 2021 16:10
Show Gist options
  • Save rafaeldcastro/7b29a395ed847edd2b875f2f5fb359d3 to your computer and use it in GitHub Desktop.
Save rafaeldcastro/7b29a395ed847edd2b875f2f5fb359d3 to your computer and use it in GitHub Desktop.
Utilities service
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UtilsService {
/**
* Turn somthing like this: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCA..."
* into a BLOB
* @param dataURI
* @returns
*/
static DataURItoBlob(dataURI: string): Blob{
const splitDataURI = dataURI.split(',');
const byteString = splitDataURI[0].indexOf('base64') >= 0 ? atob(splitDataURI[1]) : decodeURI(splitDataURI[1]);
const mimeString = splitDataURI[0].split(':')[1].split(';')[0];
const ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++)
ia[i] = byteString.charCodeAt(i)
return new Blob([ia], { type: mimeString })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment