[Vuejs]-Display an image when Blob is returned from an API

0👍

Explanation:

  1. In below snippet as you can see I am passing the objectId of the Employee fetched from Graph previously.

  2. Then making call for employee to get their Avatar/DP

  3. The Graph Profile Photo endpoint returns binary Data of the photo.

  4. Convert that binary data into data:image/png;base64,<readAsDataURL> URL e.g. data:image/png;base64,iVBORw0KGgoAAAANSU...

  5. Use in <img src="dataUrl"/>

      let imageUrl = (await request.get(GRAPH_CONFIG.GRAPH_DP_ENDPT + objectId + "/photos/48x48/\$value", { responseType: 'arraybuffer', validateStatus: (status) => status === 200 || status === 404 }))
         if (imageUrl.status === 200) {
             let reader = new FileReader()
             let blob = new Blob([imageUrl.data], {type: 'image/jpeg'})
             reader.onload = (event) => {
                 return event.target?.result.toString();
             }
             reader.readAsDataURL(blob)
         }
    

Leave a comment