javascript - 将 xmlhttprequest 中的 jpg 数据放入 <img/> 标签中

标签 javascript xmlhttprequest jpeg httpresponse binary-data

这是我的代码的一部分

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

这似乎不起作用。 我也尝试过

document.imglive.src= xmlhttp.responseText;

两者都不起作用

我在这里检查了一些提出的问题,但没有一个答案可以帮助解决这个问题。

最佳答案

使用 base64 来处理这些事情。在现代浏览器中,这个 btoa native 函数可以帮助您:

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

对于其他浏览器,有简单的模拟实现,只需检查一下即可。

P.S.:不要污染 document 对象,使用单独的变量或命名空间。

关于javascript - 将 xmlhttprequest 中的 jpg 数据放入 <img/> 标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10687427/

相关文章:

javascript - $q.all 对数组中的所有 Promise 返回相同的响应

javascript - 通过 JS 和 API 向 slack 发布消息

swift - 从 UIImage 创建 PNG 或 JPG(不用于保存在本地)

java - 在不损失质量的情况下在 JPanel 或 JLabel 中调整图像大小和显示

javascript - this.props 使用 array.map((item, i) 返回未定义

javascript - Reactjs 滚动多个引用

javascript - Ember Js : How to save a model when belongsTo attribute is dirtied?

javascript - 具有 HTTP 授权的 Chrome 扩展中的 XMLHttpRequest 继续在常规页面上发送授权 header

javascript - 无法在 XMLHttpRequest.xhr.onload (Unsplash API) 处读取未定义的属性

python - 将 jpg 图像转换为 .hdf5