29 javascript ajax extjs extjs4
这是有效.JPEG图像的二进制文件.
http://pastebin.ca/raw/2314500
我曾尝试使用Python将这个二进制数据保存到图像中.
如何将此数据转换为带有extjs 4的可查看.JPEG图像?
我试过这个,但它不起作用.
data:image/jpeg;base64,+ binary data
Run Code Online (Sandbox Code Playgroud)
Vla*_*lad 59
需要在base64中进行转换.
JS有btoa()函数.
例如:
var img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + btoa('your-binary-data');
document.body.appendChild(img);
Run Code Online (Sandbox Code Playgroud)
但我认为你在pastebin中的二进制数据是无效的 - jpeg数据必须在'ffd9'上结束.
更新:
需要将简单的十六进制写入base64转换器:
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
img.src = 'data:image/jpeg;base64,' + hexToBase64('your-binary-data');
Run Code Online (Sandbox Code Playgroud)
请参阅jsfiddle上的十六进制数据的工作示例