The atob function will decode a base64-encoded string into a new string with a character for each byte of the binary data.

const byteCharacters = atob(b64Data);

Each character's code point (charCode) will be the value of the byte. We can create an array of byte values by applying this using the .charCodeAt method for each character in the string.

const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
}

You can convert this array of byte values into a real typed byte array by passing it to the Uint8Array constructor.

const byteArray = new Uint8Array(byteNumbers);

This in turn can be converted to a Blob by wrapping it in an array and passing it to the Blob constructor.

const blob = new Blob([byteArray], {type: contentType});

The code above works. However the performance can be improved a little by processing the byteCharacters in smaller slices, rather than all at once. In my rough testing 512 bytes seems to be a good slice size. This gives us the following function.

const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }
    
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}
const blob = b64toBlob(b64Data, contentType);
const blobUrl = URL.createObjectURL(blob);

window.location = blobUrl;

Full Example:

Show code snippet

const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }
    
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}

const contentType = 'image/png';
const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

const blob = b64toBlob(b64Data, contentType);
const blobUrl = URL.createObjectURL(blob);

const img = document.createElement('img');
img.src = blobUrl;
document.body.appendChild(img);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Jeremy on Stack Overflow
Top answer
1 of 16
1181

The atob function will decode a base64-encoded string into a new string with a character for each byte of the binary data.

const byteCharacters = atob(b64Data);

Each character's code point (charCode) will be the value of the byte. We can create an array of byte values by applying this using the .charCodeAt method for each character in the string.

const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
}

You can convert this array of byte values into a real typed byte array by passing it to the Uint8Array constructor.

const byteArray = new Uint8Array(byteNumbers);

This in turn can be converted to a Blob by wrapping it in an array and passing it to the Blob constructor.

const blob = new Blob([byteArray], {type: contentType});

The code above works. However the performance can be improved a little by processing the byteCharacters in smaller slices, rather than all at once. In my rough testing 512 bytes seems to be a good slice size. This gives us the following function.

const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }
    
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}
const blob = b64toBlob(b64Data, contentType);
const blobUrl = URL.createObjectURL(blob);

window.location = blobUrl;

Full Example:

Show code snippet

const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }
    
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}

const contentType = 'image/png';
const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

const blob = b64toBlob(b64Data, contentType);
const blobUrl = URL.createObjectURL(blob);

const img = document.createElement('img');
img.src = blobUrl;
document.body.appendChild(img);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 16
666

Here is a more minimal method without any dependencies or libraries.
It requires the new fetch API. (Can I use it?)

var url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="

fetch(url)
.then(res => res.blob())
.then(console.log)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

With this method you can also easily get a ReadableStream, ArrayBuffer, text, and JSON.
(fyi this also works with node-fetch in Node)

As a function:

const b64toBlob = (base64, type = 'application/octet-stream') => 
  fetch(`data:${type};base64,${base64}`).then(res => res.blob())

But I would encourage you to don't use base64 in the first place. There are better ways to send and receive binary data. JSON isn't always the best option. it takes up more bandwidth and waste processing time (de)encodeing stuff. Us eg canvas.toBlob instead of canvas.toDataURL and use FormData to send binary files. you can also return back a multipart payload and decode it using await response.formData() that is coming from a server response. FormData can go both ways.


I did a simple performance test towards Jeremy's ES6 sync version.
The sync version will block UI for a while. keeping the devtool open can slow the fetch performance

Show code snippet

document.body.innerHTML += '<input autofocus placeholder="try writing">'
// get some dummy gradient image
var img=function(){var a=document.createElement("canvas"),b=a.getContext("2d"),c=b.createLinearGradient(0,0,1500,1500);a.width=a.height=3000;c.addColorStop(0,"red");c.addColorStop(1,"blue");b.fillStyle=c;b.fillRect(0,0,a.width,a.height);return a.toDataURL()}();


async function perf() {
  
  const blob = await fetch(img).then(res => res.blob())
  // turn it to a dataURI
  const url = img
  const b64Data = url.split(',')[1]

  // Jeremy Banks solution
  const b64toBlob = (b64Data, contentType = '', sliceSize=512) => {
    const byteCharacters = atob(b64Data);
    const byteArrays = [];
    
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      const slice = byteCharacters.slice(offset, offset + sliceSize);
      
      const byteNumbers = new Array(slice.length);
      for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }
      
      const byteArray = new Uint8Array(byteNumbers);
      
      byteArrays.push(byteArray);
    }
    
    const blob = new Blob(byteArrays, {type: contentType});
    return blob;
  }

  // bench blocking method
  let i = 500
  console.time('blocking b64')
  while (i--) {
    await b64toBlob(b64Data)
  }
  console.timeEnd('blocking b64')
  
  // bench non blocking
  i = 500

  // so that the function is not reconstructed each time
  const toBlob = res => res.blob()
  console.time('fetch')
  while (i--) {
    await fetch(url).then(toBlob)
  }
  console.timeEnd('fetch')
  console.log('done')
}

perf()
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-base64-to-blob-in-javascript
How to Convert Base64 to Blob in JavaScript? - GeeksforGeeks
September 4, 2024 - In this approach, we are going to decode a Base64-encoded image string into a binary format, convert it into a 'Blob', and create a downloadable file from it. The Base64 string is first split and decoded into binary data, which is then chunked ...
🌐
Ionic
ionic.io › home › converting a base64 string to a blob in javascript
Converting a base64 string to a blob in JavaScript - Ionic Blog
August 27, 2020 - As it turns out, it can convert data into more than just JSON, it can also return array buffers, form data, and blobs. In this case, we’ll use blobs. ... Depending on the format of the base64 string, you might need to prepend content type data.
🌐
GitHub
gist.github.com › fupslot › 5015897
Javascript: Convert base64 to a Blob · GitHub
Thanks so much :) Tremendous help for me using Expo Image Picker and uploading to Google Storage. ... function dataURIToBlob(dataURI: string) { dataURI = dataURI.replace(/^data:/, ''); const type = dataURI.match(/image\/[^;]+/)[0]; const base64 = dataURI.replace(/^[^,]+,/, ''); const arrayBuffer = new ArrayBuffer(base64.length); const typedArray = new Uint8Array(arrayBuffer); for (let i = 0; i < base64.length; i++) { typedArray[i] = base64.charCodeAt(i); } return new Blob([arrayBuffer], { type }); }
🌐
Saturn Cloud
saturncloud.io › blog › creating-a-blob-from-a-base64-string-in-javascript
Creating a BLOB from a Base64 string in JavaScript | Saturn Cloud Blog
May 1, 2026 - BLOBs are often used in databases to store large amounts of binary data. To convert a Base64 string to a BLOB in JavaScript, we can use the atob() and Blob() functions.
🌐
npm
npmjs.com › package › b64-to-blob
b64-to-blob - npm
var b64toBlob = require('b64-to-blob'); var contentType = 'image/png'; var b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACN' + 'byblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHx' + 'gljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; var blob = b64toBlob(b64Data, contentType); var blobUrl = URL.createObjectURL(blob); window.location = blobUrl;
      » npm install b64-to-blob
    
Published: Aug 21, 2017
Version: 1.2.19
🌐
GitHub
github.com › miguelmota › base64toblob
GitHub - miguelmota/base64toblob: Base64 string to Blob object
May 24, 2022 - var base64 = 'iVBORw0...ASUVORK5CYII='; var blob = base64ToBlob(base64, 'image/png'); var url = window.URL.createObjectURL(blob); console.log(url); // blob:http://localhost:8888/2c6321f4-8f4e-457f-8b4e-2c3932b4bef0
Author: miguelmota
Find elsewhere
🌐
npm
npmjs.com › package › base64-blob
base64-blob - npm
July 23, 2019 - A module for interconversion of base64 and blob, wrapped by Promise. Latest version: 1.4.1, last published: 7 years ago. Start using base64-blob in your project by running `npm i base64-blob`. There are 11 other projects in the npm registry ...
      » npm install base64-blob
    
Published: Jul 23, 2019
Version: 1.4.1
🌐
Oracle
forums.oracle.com › ords › apexds › post › base64-to-blob-4641
Base64 to Blob - Oracle Forums
April 5, 2010 - Hi guys, I am not an oracle expert. I am trying to insert a base64 data into a Blob. I was trying to use : INSERT INTO MyDB.EXCELFILETABLE(FILELID,SPREADSHEETDATA,OWNER) values ( 1, utl_encode.base...
🌐
GitHub
gist.github.com › Marceloalves6 › d1f590437f7fe45ac6832e5177c84be6
Typescript method to convert string base64 to blob · GitHub
Typescript method to convert string base64 to blob · 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.
🌐
GitHub
gist.github.com › poeticninja › 0e4352bc80bc34fad6f7
base64 to file blob, very basic example. · GitHub
September 18, 2019 - 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.
🌐
Base64Encode.org
base64encode.org › enc › blob
Base64 Encoding of "blob" - Online
Split lines into chunks: The encoded data will become a continuous text without any whitespaces, so check this option if you want to break it up into multiple lines. The applied character limit is defined in the MIME (RFC 2045) specification, which states that the encoded lines must be no more than 76 characters long. (*) Perform URL-safe encoding: Using standard Base64 in URLs requires encoding of "+", "/" and "=" characters into their percent-encoded form, which makes the string unnecessarily longer.
🌐
DEV Community
dev.to › ionic › converting-a-base64-string-to-a-blob-in-javascript-35kl
Converting a base64 string to a blob in JavaScript - DEV Community
September 30, 2020 - As it turns out, it can convert data into more than just JSON, it can also return array buffers, form data, and blobs. In this case, we'll use blobs. ... Depending on the format of the base64 string, you might need to prepend content type data.
🌐
Quora
quora.com › What-is-the-difference-between-Blob-and-Base64
What is the difference between Blob and Base64? - Quora
Answer (1 of 5): Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding. Each Base64 digit represents exactly 6 bits of da...
🌐
PTC Community
community.ptc.com › home › iot & connectivity › thingworx › thingworx developers › base64 to blob via base64decodebytes gives error
base64 to blob via base64DecodeBytes gives error | Community
March 12, 2024 - In your case, I wonder where the problem is, if it is base64 and you can decode it, it shouldn't matter if it is an image or a PDF or any other binary format. Here's some sample code to encode and restore: ... function encodeBase64(repo, path) { let s = repo.LoadBinary({path:path}); var blobArray = []; logger.warn("s.length="+s.length); for (var i = 0; i < s.length; i++) blobArray.push(s[i]); b64=base64EncodeBytes({"array":blobArray}); repo.SaveText({ path: path+".b64", content: b64}); return b64; } result=encodeBase64(Things["SystemRepository"],"thumbnail.png");
🌐
IQCode
iqcode.com › code › other › how-to-convert-a-base-64-to-blob
how to convert a base 64 to blob Code Example
October 2, 2021 - import { base64StringToBlob } from 'blob-util'; const contentType = 'image/png'; const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const blob = base64StringToBlob(b64Data, contentType); // Do whatever you ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › FileReader › readAsDataURL
FileReader: readAsDataURL() method - Web APIs | MDN
September 18, 2025 - The readAsDataURL() method of the ... is used to read the contents of the specified Blob or File. When the read operation is finished, the readyState property becomes DONE, and the loadend event is triggered. At that time, the result attribute contains the data as a data: URL representing the ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › convert-base64-to-blob-7497
convert base64 to blob - Oracle Forums
October 25, 2023 - Hello everybody. Please help me. I have a problem, i have a large base64, now i would like to base64 convert to blob and insert the table. but this process give error because of utl decode package has...