将xmlhttprequest中的jpg数据放入< img/>标签 [英] put jpg data from xmlhttprequest into <img /> tag

查看:97
本文介绍了将xmlhttprequest中的jpg数据放入< img/>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我代码的一部分

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;

都不起作用

我在这里检查了一些询问的问题,但在这个问题上没有任何帮助的答案.

I checked some of the asked questions here but none of the answers where helping at this porblem.

推荐答案

使用base64进行这些操作.在现代浏览器中,有一个btoa本机函数可以为您提供帮助:

Use 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 .:请勿污染document对象,请使用单独的变量或命名空间.

P.S.: don't pollute the document object, use a separate variable or a namespace.

这篇关于将xmlhttprequest中的jpg数据放入&lt; img/&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆