23

I am storing the image in a base64 format in a node. Then I am receiving it and storing in a variable. and show it in the tag but it is not showing. Correct values are receiving from server. In render, function condition is true if the state is set.even if its true its not showing.

getImage() {
    console.log('getImage');
    axios.get(`http://localhost:4000/image/get`).then((result) => {
        this.setState({ image: result })
        console.log(result);
    });
}

render(){
    {this.state.image ? <img src={this.state.image}></img>: ''}
}

I am getting exact base64 string which i am storing in the server.It returns

<img src="[object Object]">

in DOM.I don't know where am I going wrong

EDIT

router.route('/image/get').get((req, res) => {
    console.log('inside img get');
    Image.find((err, result) => {
        if (err) {
            res.json({ "error": true, "message": "error fetching data" });
        } else {
            // console.log(result);
            res.json(result);
        }
    })

});

model

const mongoose=require('mongoose');
const Schema=mongoose.Schema;
var ImageSchema=new Schema({
    imageName:{
        type:String
    },
    imageData:{
        type:String
    }

});

export default mongoose.model('Image', ImageSchema);
Karthi
  • 3,019
  • 9
  • 36
  • 54

5 Answers5

29

Did you make sure to add the encoding type in the src attribute along with the base64 encoded value?

render(){
    {this.state.image ? <img src={`data:image/png;base64,${this.state.image}`}/>: ''}
}
Matías Bustos
  • 348
  • 3
  • 8
  • 3
    tried. nothing showed up.it returns . the value in this.state.image is "data:image/png;base64,iVBORw0KGgoAAAAN......." – Karthi Jun 26 '19 at 09:21
  • @Krazy the thing is that the *src* of the actual image might be nested in the response *object* – Lakshaya U. Sep 04 '21 at 07:18
3
const byteCharacters = atob(result);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);

let image = new Blob([byteArray], { type: 'image/jpeg' });

Once you have the Blob you need to convert that into a data url like this:

let imageUrl = URL.createObjectURL(image);
this.setState({image: imageUrl});

now u can use this url saved in your state as a source for image like this:

<img src={this.state.image} />

Hope this helps!!

Harshit Agarwal
  • 2,308
  • 2
  • 15
  • 23
2

When you get this error:

<img src="[object Object]">

this means you insert an object to the src instead of a string. my guess is that your response returns an object and not string

 axios.get(`http://localhost:4000/image/get`).then((result) => {
        this.setState({ image: result })
        console.log(result);
    });

check if the result is an object

1

The answer given by matias bustos works correctly only just that you need to add height and width to the image tag like this:

render(){
   {this.state.image ? 
      <img width='500' height='200' src={`data:image/png;base64,${this.state.image}`}/>: 
   ''}
}
F Blanchet
  • 1,430
  • 3
  • 21
  • 32
1

You can't directly append base64 string to image source. First you have to decode it using Buffer then append that data in the src.

Try like this.

decodeBase64(base64data) {
    let base64ToString = Buffer.from(base64data, "base64").toString()
    this.setState({data: base64ToString })
}

render() {
    return (
       <img src={"data:image/jpeg;base64," + this.state.data} />
    )
}

base64data is the data in base64 format.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81