Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

469
Views
Javascript: Temporarily store and display image received from backend as binary data?

I am currently trying to display an image, which I receive from a backend server in a particular way/format, on the screen of the browser.

My problem is acutally closely related to this issue, for which no real answer exists.

Here is a screenshot displaying what the backend server's response looks like:

enter image description here

payload.data contains the data of the image, which is a green cloud (also attached at the end of this post for reference).

My first, probably very stupid, question would be: What kind of format/encoding is that?

Anyway, here is what I then further tried to process the data:

        const blob = new Blob([action.payload.data], { //contains the data
            type: action.payload.headers["content-type"] // 'image/png'
        })
        console.log("blob: ", blob);
        const url = URL.createObjectURL(blob);
        console.log("url : ", url)

As a result, the blob is sucessfully created, as well as the url. However, when I open that link, no image gets displayed.

I am stuck here and would appreaciate any kind of helpful hint pointing out where I am doing a mistake here.

Thanks very much for your support in advance.

PS: As promised, here is the actual png image: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like your data attribute is still in binary format. You need to convert the hex into base64 in order to display the image. First, if the server you're fetching the image form is yours, I would recommend encoding the image on the server before sending it to the client. If the server is not yours and you can't change the data that is being returned, try something like this:

function hexToBase64(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}

And then use it like this:

const img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + hexToBase64('your-binary-data');
document.body.appendChild(img);

reference: How to display binary data as image - extjs 4

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!