Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:10:21+00:00 2026-05-26T17:10:21+00:00

var xhr = new XMLHttpRequest(); xhr.open(‘GET’, ‘http://static.reddit.com/reddit.com.header.png’, true); xhr.responseType = ‘arraybuffer’; xhr.onload = function(e)

  • 0
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://static.reddit.com/reddit.com.header.png', true);

xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
  if (this.status == 200) {
    var uInt8Array = new Uint8Array(this.response);
    var byte3 = uInt8Array[4]; 

    var bb = new WebKitBlobBuilder();
    bb.append(xhr.response);
    var blob = bb.getBlob('image/png'); 
    var base64 = window.btoa(blob);
    alert(base64);

  }
};

xhr.send();

Basically, what I am trying to do here is retrieve an image, and convert it to base64.

From reading in the comments here, it states:

“Sure. After fetching a resource as an ArrayBuffer, create a blob from
it. Once you have that, you could base64 encode the file/blob directly
window.btoa() or FileReader.readAsDataURL().”

However, blob is just [object blob], while I need to get the binary from the image so I can convert it to base64 and display it in a img tag using data.

Anyone know how to achieve this?

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:10:22+00:00Added an answer on May 26, 2026 at 5:10 pm

    Don’t use BlobBuilder in Chrome (tested in OSX Chrome, Firefox 12, Safari 6, iOS Chrome, iOS Safari):

    ex1 : http://jsfiddle.net/malraux/xGUsu/ (principle)

    ex2: http://jsfiddle.net/xGUsu/78/ (working with full example)

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'doodle.png', true);
    
    xhr.responseType = 'arraybuffer';
    
    // Process the response when the request is ready.
    xhr.onload = function(e) {
      if (this.status == 200) {
        // Create a binary string from the returned data, then encode it as a data URL.
        var uInt8Array = new Uint8Array(this.response);
        var i = uInt8Array.length;
        var binaryString = new Array(i);
        while (i--)
        {
          binaryString[i] = String.fromCharCode(uInt8Array[i]);
        }
        var data = binaryString.join('');
    
        var base64 = window.btoa(data);
    
        document.getElementById("myImage").src="data:image/png;base64," + base64;
      }
    };
    
    xhr.send();
    

    Note: This code is over 7 years old at this point. While it should still function in most browsers, here’s an updated version based on a suggestion by @TypeError that will only work in more modern browsers with the possible exception of iOS Safari (which may or may not support responseType = 'blob' – make sure to test!):

    var xhr = new XMLHttpRequest();
    xhr.open('get', 'doodle.png', true);
    
    // Load the data directly as a Blob.
    xhr.responseType = 'blob';
    
    xhr.onload = () => {
      document.querySelector('#myimage').src = URL.createObjectURL(this.response);
    };
    
    xhr.send(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such part of code: var xhr = new XMLHttpRequest(); xhr.open(POST, http://someurl.com, true);
i have the following javascript in my webpage: var xhr = new XMLHttpRequest(); xhr.open('GET',
I saw this code sample without any explanation: var xhr = new XMLHttpRequest(); xhr.open('GET',
I've got the following synchronous request... var xhr = new XMLHttpRequest(); xhr.open(GET,uri,false); xhr.send(null); I've
function connectTo(url) { var xhr = new XMLHttpRequest(); xhr.open(GET, url, false); xhr.onreadystatechange = function
function fetchXmlDoc(uri) { var xhr = new XMLHttpRequest(); var async = false; xhr.open(GET, uri,
There is this function: function uploadFile(f, parent) { var xhr = new XMLHttpRequest(); xhr.upload.addEventListener(progress,
Is it possible to send an xhr HTTP HEAD request to only get header
Why xhr.responseXML.getElementsByClassName('clazz') doesn't work? Here is the js: var xhr = new XMLHttpRequest(); xhr.onreadystatechange
My Javascript function request to a aspx page. İts Code: var xhr = (XMLHttpRequest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.