admin管理员组文章数量:1022784
here is some part of my code
xmlhttp.open("GET", theUrl, true);
document.imglive.innerHTML = '<img src="data:image/jpeg,' + xmlhttp.responseText + '"/>';
that don´t seem to work. i also tried
document.imglive.src= xmlhttp.responseText;
that neither worked
I checked some of the asked questions here but none of the answers where helping at this porblem.
here is some part of my code
xmlhttp.open("GET", theUrl, true);
document.imglive.innerHTML = '<img src="data:image/jpeg,' + xmlhttp.responseText + '"/>';
that don´t seem to work. i also tried
document.imglive.src= xmlhttp.responseText;
that neither worked
I checked some of the asked questions here but none of the answers where helping at this porblem.
Share Improve this question asked May 21, 2012 at 14:42 GobliinsGobliins 4,04618 gold badges71 silver badges130 bronze badges 8-
what does
responseText
contain? – Joseph Commented May 21, 2012 at 14:44 -
2
Why aren't you just doing
document.imglive.src='/new/src.jpg';
? – paulslater19 Commented May 21, 2012 at 14:45 - @Joseph some jpeg data like �����JFIF���������...�*C���NC�]���,O��QE� – Gobliins Commented May 21, 2012 at 14:48
-
Aside from the reason, the
document.imglive.src = "data:image/jpeg,..."
should be in theonreadystatechange
-handler. – Andreas Commented May 21, 2012 at 14:49 - @paulslater i am reading also the header data out of the xmlhttpresponse object if i would make a second request the data may have changed. i need header data and content from one request. – Gobliins Commented May 21, 2012 at 14:50
2 Answers
Reset to default 3Use base64 for these things. In modern browsers there's this btoa
native function that may help you:
document.imglive.innerHTML = "<img src='data:image/jpeg;base64," + btoa(xmlhttp.responseText) + "'/>";
For other browsers there are simple emulated implementations, just check them out.
P.S.: don't pollute the document
object, use a separate variable or a namespace.
If you are happy with IE10+, you can use xhr.responseType = 'blob'
in conjunction with window.URL.createObjectURL()
(to get free support for getting the correct mime type).
xhr.responseType = 'blob';
xhr.onload = function(response) {
var url = window.URL.createObjectURL(response);
document.imglive.src = url; // from your example code
}
xhr.open("GET", theUrl, true);
here is some part of my code
xmlhttp.open("GET", theUrl, true);
document.imglive.innerHTML = '<img src="data:image/jpeg,' + xmlhttp.responseText + '"/>';
that don´t seem to work. i also tried
document.imglive.src= xmlhttp.responseText;
that neither worked
I checked some of the asked questions here but none of the answers where helping at this porblem.
here is some part of my code
xmlhttp.open("GET", theUrl, true);
document.imglive.innerHTML = '<img src="data:image/jpeg,' + xmlhttp.responseText + '"/>';
that don´t seem to work. i also tried
document.imglive.src= xmlhttp.responseText;
that neither worked
I checked some of the asked questions here but none of the answers where helping at this porblem.
Share Improve this question asked May 21, 2012 at 14:42 GobliinsGobliins 4,04618 gold badges71 silver badges130 bronze badges 8-
what does
responseText
contain? – Joseph Commented May 21, 2012 at 14:44 -
2
Why aren't you just doing
document.imglive.src='/new/src.jpg';
? – paulslater19 Commented May 21, 2012 at 14:45 - @Joseph some jpeg data like �����JFIF���������...�*C���NC�]���,O��QE� – Gobliins Commented May 21, 2012 at 14:48
-
Aside from the reason, the
document.imglive.src = "data:image/jpeg,..."
should be in theonreadystatechange
-handler. – Andreas Commented May 21, 2012 at 14:49 - @paulslater i am reading also the header data out of the xmlhttpresponse object if i would make a second request the data may have changed. i need header data and content from one request. – Gobliins Commented May 21, 2012 at 14:50
2 Answers
Reset to default 3Use base64 for these things. In modern browsers there's this btoa
native function that may help you:
document.imglive.innerHTML = "<img src='data:image/jpeg;base64," + btoa(xmlhttp.responseText) + "'/>";
For other browsers there are simple emulated implementations, just check them out.
P.S.: don't pollute the document
object, use a separate variable or a namespace.
If you are happy with IE10+, you can use xhr.responseType = 'blob'
in conjunction with window.URL.createObjectURL()
(to get free support for getting the correct mime type).
xhr.responseType = 'blob';
xhr.onload = function(response) {
var url = window.URL.createObjectURL(response);
document.imglive.src = url; // from your example code
}
xhr.open("GET", theUrl, true);
本文标签: javascriptput jpg data from xmlhttprequest into ltimg gt tagStack Overflow
版权声明:本文标题:javascript - put jpg data from xmlhttprequest into <img > tag - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745507367a2153666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论