Yes, we obtain corrupted images in result, but actually base64 encoded string ,returned from Base64.encode() method (link to js library above), is corrupted. I have compared string, returned from php function base64_encode, and string, returned from Base64.encode() method. They are not the same.
I found the solution using PHP. Here it is.
Using this javascript we send the AJAX request to the php script that returns base64 encoded image binary data:
var xmlhttp = getXmlHttp();
xmlhttp.open('GET', 'http://example.com/script.php', true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
//Here we embed base64 encoded data into html
document.getElementById('content').innerHTML = '<img src="data:image/jpeg; base64,' + xmlhttp.responseText + '" />';
}
}
};
xmlhttp.send(null);
Here script.php is:
<?php
echo base64_encode(file_get_contents('image.jpeg'));
?>
I also tried to use only JS, but it seems that functions that I had found, for example here http://www.webtoolkit.info/javascript-base64.html , don't encode correctly. Although may be I missed something.
Hi to all! I need some help with putting image binary data into html.
var xmlhttp = getXmlHttp();
xmlhttp.open('GET', 'http://example.com/image.jpeg', true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
//Here, when AJAX request will be completed, i would like to put data conrained in xmlhttp.responseText into html and make browser correctly render it.
}
}
};
xmlhttp.send(null);
Any help is appreciated.