zzz / frontend /src /utils /base64-to-blob.ts
ar08's picture
Upload 1040 files
246d201 verified
raw
history blame contribute delete
743 Bytes
export const base64ToBlob = (base64: string) => {
// Remove the prefix (e.g. data:image/png;base64,)
const base64WithoutPrefix = base64.split(",")[1] || base64;
// Decode to bytes
const bytes = atob(base64WithoutPrefix);
// Process in chunks to avoid memory issues
const chunkSize = 8192; // Process 8KB at a time
const chunks = [];
for (let i = 0; i < bytes.length; i += chunkSize) {
const chunk = bytes.slice(i, i + chunkSize);
const array = new Uint8Array(chunk.length);
for (let j = 0; j < chunk.length; j += 1) {
array[j] = chunk.charCodeAt(j);
}
chunks.push(array);
}
// Create a Blob from all chunks
return new Blob(chunks, { type: "application/zip" });
};