How to display mysql blob image in html using Vuejs?
Asked Answered
R

3

7

I have a vue file like this,

export default {
	data(){
		return{
            info: {
                name: '',
                image: '',
              
            },
            errors: []
		}
	},
  
  created: function(){
        this.getInfo();
  },
  
  methods: {
        getInfo: function(){
              this.info.name = response.data.results[0].name;
              this.info.image = response.data.results[0].image;
        }
  }
}

I am passing data from this file into a child Vue component. The child component is as follows,

<template>
    <div class="ui items">
        <div class="item">
            <div class="ui small image">
                {{info.image}}
            </div>
        </div>
    </div>
  
</template>

<script>
export default{

    props:['info']

}
</script>

My image is stored as a blob in a MySQL database. When I run my application, the image appears as binary data on the UI. The object appears like this,

JSON response of the image

Can anybody here help me in displaying the image? Thank you very much!

Roveover answered 24/11, 2017 at 7:51 Comment(0)
S
8

What you want is a data url. You will need to convert the byte array to base64. There is no way to use the raw bytes. Perhaps do this in a computed property, using one of the byte array to base64 functions.

Markup

<img :src="dataUrl">

Behaviour (untested!)

computed : {
    dataUrl(){
        return 'data:image/jpeg;base64,' + btoa(
            new Uint8Array(this.info.image)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        );
    }
}

Search your conscience. This is really not a good idea :-) Sending images as a JSON encoded byte array is something I've never seen, and will be about, guessing, 10x larger on the wire than the binary image. Images in the DB are an antipattern. Images in JSON work, but they should be encoded as base64 strings in the JSON. Even then, they reduce the readability of the JSON, and can bury tools like Postman. Data urls are much slower to load than regular urls. Even with images in the DB, if you control your api, you can gain a lot by making image apis that return just the byte array, with an application/jpeg mime type.

Symbolist answered 24/11, 2017 at 8:10 Comment(1)
Please check my post. It has been updated. I have shared what the JSON response of the image looks likeRoveover
Q
3

You can load image data using Base64 format, like this,

<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0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FeEGPIKfYU8AETkCYI6wANkZQwIKRNDB1hA0Jrf0rk6WXZ8BvWkb4kv99vn89kDrfVexBSYgVNwDA7AN+jAK3gEd+AlGMGIBFDgFvzouK3JV/lihQTOwLtOtw9wIRG5pJn91Tbgqk9kSk7GViADrTD4HCyZ0NQnomi51sb0fUyCMQEbp2WpU67IjfNjwcYyoUDhjJVcZBjYBy40j4wXgaobWoe8Z6Y80CJBwFpunepIzt2AUgFjtXXshNXjVmMh+K+zzp/CMs0CqeuzrxSRpbOKfdCkiMTS1VBQ41uxMyQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=">

For your problem,

export default {
    data(){
        return{
            info: {
                name: '',
                image: '',

            },
            errors: []
        }
    },

  created: function(){
        this.getInfo();
  },

  methods: {
        getInfo: function(){
              this.info.name = response.data.results[0].name;
              this.info.image = 'data:image/jpeg;base64,' + btoa(response.data.results[0].image.data);
        }
  }
}

In template,

<template>
    <div class="ui items">
        <div class="item">
            <div class="ui small image">
                <img :src="info.image">
            </div>
        </div>
    </div>
</template>
Questa answered 24/11, 2017 at 7:55 Comment(6)
It certainly is a solution, but not very efficient. This is a tedious task. There are multiple images that need to be loaded from the database in the future. is there any built-in functions I could use? Thanks!Roveover
Not sure there is better way. But to make it more clear take a look here.Till
Not sure about better way, but you can check this one also https://mcmap.net/q/1481065/-binding-img-srcQuesta
@Till Thanks. Please check my post. It has been updated. I have shared what the JSON response of the image looks like.Roveover
@CasperSL Please check my post. It has been updated. I have shared what the JSON response of the image looks likeRoveover
check this #14915558Questa
I
-1
<img :src="require(`../../assets/images/products/${product.img}`)" />
Inequity answered 28/3, 2020 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.