I am trying to get an image (PNG format) to display in a React app after making a JavaScript call. The code is as follows. The function DeviceService.getFile returns the file in a blob. The data is binary. How can I get this image to be displayed correctly in React?
I have tried the conversion to base64 but it did not help.
DeviceService.getFile(mapOverlayImagePath, bMap1 => {
this.setState({ MapOverlay: bMap1 })
// this.setState({ MapOverlay: 'data:image/png;base64,' + btoa(bMap1) })
console.log(bMap1)
})
The React code to display the image:
<img src={this.state.MapOverlay} alt="MapOverlay" />
I have modified this function, the function getFile is as follows:
export function getFile(path, cb) {
if (typeof(cb) === 'undefined' || cb === null)
return;
fetch(uris.getFile() + '/?' +
'path=' + path,
{method: 'POST', credentials: 'include'})
.then(reply => reply.blob())
.then((response) => {
if (response.data) {
return cb(response.data);
}
return cb(new Blob());
})
}
This getFile function is in a library where it is used as a dependency in the React application.
I tried printing the blob size in the Internet Explorer console and it displayed: [object Blob]: {size: 0, type: ""}
. I guess my getFile function is not passing the data as intended. Any error in the getFile function?