Fetch error when convert base64 to blob on react-native

Viewed 720

This error occurs when I run the following code in React Native to convert base64 to blob on Android

let url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...';
let res = await fetch(url);
let blob = await res?.blob();

enter image description here

2 Answers

which react-native-version are you using ?

const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...';

const getBlob = async (uri) => {
  const res = await fetch(uri);
  return res.blob();
};

const blob = await getBlob(url);

Should work

If you want to convert Base64 to blob you can use the following way :

Install the package below

npm install buffer --save

First, convert your Base64 to the array of bytes

import { Buffer } from "buffer";


const base64 = 'iVBORw0KGgoAAAANSUhEU ....'
let your_bytes = Buffer.from(base64, "base64");

Then convert it to blob:

const blob = new Blob([your_bytes], { type: 'YOUR TYPE' })
Related