Heisenberg
Heisenberg

Reputation: 71

Fetch error when convert base64 to blob on react-native

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

Upvotes: 3

Views: 2405

Answers (2)

Abolfazl Roshanzamir
Abolfazl Roshanzamir

Reputation: 14802

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' })

Upvotes: 1

Pasindu Dilshan
Pasindu Dilshan

Reputation: 386

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

Upvotes: 0

Related Questions