File size: 829 Bytes
6821e3e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
export async function blobToDataUri(blob: Blob, defaultContentType = ""): Promise<string> {
if (typeof window === "undefined") {
const arrayBuffer = await blob.arrayBuffer()
let buffer = Buffer.from(arrayBuffer)
return "data:" + (defaultContentType || blob.type) + ';base64,' + buffer.toString('base64');
} else {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.onload = _e => {
let dataUri = `${reader.result as string || ""}`
if (defaultContentType) {
dataUri = dataUri.replace("application/octet-stream", defaultContentType)
}
resolve(dataUri)
}
reader.onerror = _e => reject(reader.error)
reader.onabort = _e => reject(new Error("Read aborted"))
reader.readAsDataURL(blob)
});
}
} |