First, you need to fetch your image with the response type arraybuffer
. Then you can convert the result to a base64 string and assign it as src
of an image tag. Here is a small example with React.
import React, { Component } from 'react';
import axios from 'axios';
class Image extends Component {
state = { source: null };
componentDidMount() {
axios
.get(
'https://www.example.com/image.png',
{ responseType: 'arraybuffer' },
)
.then(response => {
const base64 = btoa(
new Uint8Array(response.data).reduce(
(data, byte) => data + String.fromCharCode(byte),
'',
),
);
this.setState({ source: "data:;base64," + base64 });
});
}
render() {
return <img src={this.state.source} />;
}
}
export default Image;