Mar*_*arc 11 html javascript binary dom xmlhttprequest
我正在通过XMLHttpRequest以二进制格式从我的服务器加载jpeg图像(我需要这样).它不是base64编码的.
是否可以使用javascript将其转换为img对象?
谢谢
如果的字符编码XMLHttpRequest已被设置为东西不会改变二进制数据,或者你已经设置的响应类型,你可以运行.responseText通过btoa(把它用base64,让你将其指定为数据URI)或分别访问.response二进制数据。
假设您的实例已命名xhr并且您xhr.send在xhr.opendo之前使用 charset 方法
xhr.overrideMimeType("text/plain; charset=x-user-defined");
Run Code Online (Sandbox Code Playgroud)
那么当你 200 OK
var dataURI = 'data:image/jpeg;base64,' + btoa(xhr.responseText);
Run Code Online (Sandbox Code Playgroud)
然后您可以设置为SRC的<img>。
再次假设xhr,这次.response方法;.open和之间.send,
xhr.responseType = "arraybuffer";
Run Code Online (Sandbox Code Playgroud)
然后在 200 OK
var arrayBufferView = new Uint8Array(xhr.response), // can choose 8, 16 or 32 depending on how you save your images
blob = new Blob([arrayBufferView], {'type': 'image\/jpeg'}),
objectURL = window.URL.createObjectURL(blob);
Run Code Online (Sandbox Code Playgroud)
然后您可以设置为SRC的<img>。例子
| 归档时间: |
|
| 查看次数: |
5701 次 |
| 最近记录: |