PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

[FIXED] How can I turn the data that is a image came from API to url to use in img tag in react Js

 May 05, 2022     api, image, javascript, reactjs     No comments   

Issue

I have uploaded an image. Now, I have a download API that gives me this image as a data. The response looks like this:

enter image description here enter image description here

the header of the response is like this:

enter image description here

Now the problem is that I want to show this image on the web page using img tag or anything else possible in react js. Passing the content to src attr shows nothing. Does anyone have any ideas? Is there any possible way?


Solution

You should be able to use the FileReader to convert the response to base64 format which can be used in the src attribute:

// Request was successful, get image blob
const blob = await response.blob()

// Convert the blob to base64
const reader = new FileReader()
reader.readAsDataURL(blob)

// Get the converted image and set it where needed
reader.onloadend = () => {
    const base64Image = reader.result
    // Set the image to the src of the image tag or setState as you need
}


Answered By - Jacob
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing