29

Can someone help me display a Base 64 encoded image using vue.js?

Basically I have an image object:

img = {
  encodedImage: '/9x/4AFQSkZJRgABAXAASABIAAD...'
}

I know that in pure HTML i can do something like:

<div>
    <p>Taken from wikpedia</p>
    <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div> 

In vue, i have tried the following:

<img :src="img.encodedImage" />
<img v-bind:src="img.encodedImage" />
<img :src="{{img.encodedImage}}" />
<img v-bind:src="{{img.encodedImage}}" />

Here's my full vue component:

<template>
  <div>
    <img :src="img.encodedImage">
  </div>
</template>
<script>

export default {
  props: [ 'img' ]
}
</script>

Can someone help?

Thanks in advance!

6
  • Did you set img to be a property of the vue instance? Or are you just trying to reference a plain javascript object? Commented Sep 29, 2017 at 15:39
  • it's part of my vue instance @thanksd Commented Sep 29, 2017 at 15:41
  • Can you show how you've set that in your vue instance? Commented Sep 29, 2017 at 15:42
  • sure - just updated my question. note - the img object comes in as a prop Commented Sep 29, 2017 at 15:45
  • 3
    And how are you passing img to the component? BTW this is working for me jsfiddle.net/1eddx72k Commented Sep 29, 2017 at 15:51

6 Answers 6

49

In vue.js, it'll look like:

<img v-bind:src="'data:image/jpeg;base64,'+imageBytes" />
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me with slight change - <img v-bind:src="imageBytes" />
19

To display any data URI in a vue component you can do something like this:

Data URI way

<template>
  <img :src="`data:image/png;base64,${img.encodedImage}`" />
</template>
export default {
  props: ['img'],
};

Recommended way

<template>
  <img :src="image" />
</template>
import image from '@/assets/image.png';

export default {
  data() {
    return {
      image,
    };
  },
};

1 Comment

Even though it's recommended, it's not practical in every situation. I created a Vue app, integrated it with a CRM and my images did not show (status 404) because of incorrect paths.The data-uri way solved my problem.
3

Have had the same issue.

Change

img = {  encodedImage: '/9x/4AFQSkZJRgABAXAASABIAAD...'}

to

img = {  encodedImage: 'data:image/jpg;base64,/9x/4AFQSkZJRgABAXAASABIAAD...'}

should solve the problem.

Comments

1

<img :src="'data:image/png;base64,' + item.ItemImageData ">

Here, item.ItemImageData contains base64 image string. As my image is png hence I used "data:image/png". Change it accordingly.

Comments

0

Here's a reusable image component:

<template>
  <img v-bind:src="'data:image/gif;base64,'+ imageAsBase64" />
</template>

<script>
import fs from "fs";

export default {
  data() {
    return {
      imageAsBase64: ""
    };
  },
  mounted() {
    this.imageAsBase64 = fs.readFileSync("./assets/progress-bar.gif", "base64");
  }
};
</script>

Then use in your components:

<template>
  <div>
    <DataImage></DataImage>
  </div>
</template>

<script>
import DataImage from "./DataImage.vue";

export default {
  components: {
    DataImage
  }
}
</script>

Comments

0

For large images, putting the image content into different js files works for me.

--- myImage.js content ---

export default {
  getContent() {
    return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABCkAAAKkCAY ...";
  }
}

--- index.vue content ---

import myImage from "../assets/base64/myImage"

export default {
  name: "IndexPage",
  data() {
    return {
      myImageBase64: myImage.getContent(),
  }
}

--- template content ---

<template>
<div class="home-dashboard">
   <img :src="myImageBase64" alt="" class="img-fluid rounded-md shadow-md">
</div>
</template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.