Load external binary data as data url


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var xmlhttp = new XMLHttpRequest();
  2. xmlhttp.open("GET",url,false);
  3. xmlhttp.overrideMimeType('text/plain; charset=x-user-defined');
  4. xmlhttp.send();
  5. var data = '';
  6. for ( var i = 0; i < xmlhttp.responseText.length; i++ ) data += String.fromCharCode( ( xmlhttp.responseText[ i ].charCodeAt(0) & 0xff ) );
  7. // Convert raw data to base64
  8. data = btoa( data );
  9. var base64src='data:image/jpg;base64,'+data; // change the MIME type according to content

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.