javascript如何通过服务器给到的字节码字符串,显示一张图片?

(先请大家不要抨击这种设计如何,请各位正面回答问题,感激不尽!)
服务器保存的是图片的二进制字符数组(varchar),如何在浏览器上面显示?
按照我的理解:
首先获取到字符串之后应该转换成字节数组,字符转字节的方式是怎样的。
其次就是js怎么把这个字节数组展示成图片,在一个div中。
不知我理解的流程是否正确,每个流程中的难点应该如何攻破,求高手指点。

已邀请:

cc25937795

赞同来自:

只要服务器配置了 Content-Type,直接输出字节码就可以,对客户端而言和图片文件没什么区别。

如果非要是一个 JS 中的字符串,就要先进行 BASE64 编码,然后生成 data URL,最后生成 img 对象,可以参考 SO 上的 Display image from http response with image content type

zjq19921101

赞同来自:

var img = new Image();
img.src='http://ubmcmm.baidustatic.com/media/v1/0f0005yYIsGLBGDGfPud10.png';
document.body.appendChild(img);

把图片地址换成你所谓的服务端接口即可。

cnhfz2016

赞同来自:

直接把你的字节码放在img的src中就可以实现。你可以把下面代码是src放入游览器中,也可以直接访问。

<img src="data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA8AAD/4QN6aHR0
cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1
TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5z
Om1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjUtYzAxNCA3OS4xNTE0ODEsIDIw
MTMvMDMvMTMtMTI6MDk6MTUgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDov
L3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRp
b24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8x
LjAvbW0vIiB4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