把JPG数据来自XMLHttpRequest的成 标签(put jpg data from x

2019-09-17 09:27发布

这里是我的一些代码的一部分

xmlhttp.open("GET", theUrl, true);
document.imglive.innerHTML = '<img src="data:image/jpeg,' + xmlhttp.responseText + '"/>';

是鸵鸟政策似乎工作。 我也尝试

document.imglive.src= xmlhttp.responseText;

这既不工作

我查了一些问的问题,在这里,但这里没有这个porblem帮助的答案。

Answer 1:

使用base64,以这些东西。 在现代浏览器有这个btoa机功能,可以帮助你:

document.imglive.innerHTML = "<img src='data:image/jpeg;base64," + btoa(xmlhttp.responseText) + "'/>";

对于其他浏览器也有简单模拟的实现,刚刚检查出来。

PS:不污染document对象,使用一个单独的变量或命名空间。



Answer 2:

如果你是幸福的IE10 +,你可以使用xhr.responseType = 'blob'连同window.URL.createObjectURL()获得用于获取正确的MIME类型的免费支持)。

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);


文章来源: put jpg data from xmlhttprequest into tag