Last active
December 10, 2021 07:48
-
-
Save mariusbolik/60fd1281ead7fd7ed0394fbbd4b4f4e1 to your computer and use it in GitHub Desktop.
Angular - Download Image as Base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async downloadImageToBase64(imageUrl: string) { | |
const resp: Blob = await this.httpClient.get(imageUrl, | |
{ | |
headers: { | |
accept: 'image/png, image/webp, image/jpeg, image/gif' | |
}, | |
responseType: 'blob' | |
}).toPromise(); | |
return (await this.convertBlobToBase64(resp)) as string; | |
} | |
private convertBlobToBase64(blob: Blob) { | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onerror = reject; | |
reader.onload = () => { | |
resolve(reader.result); | |
}; | |
return reader.readAsDataURL(blob); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment