<small id='rdZ3Y'></small><noframes id='rdZ3Y'>

    • <bdo id='rdZ3Y'></bdo><ul id='rdZ3Y'></ul>
  • <legend id='rdZ3Y'><style id='rdZ3Y'><dir id='rdZ3Y'><q id='rdZ3Y'></q></dir></style></legend>

    <i id='rdZ3Y'><tr id='rdZ3Y'><dt id='rdZ3Y'><q id='rdZ3Y'><span id='rdZ3Y'><b id='rdZ3Y'><form id='rdZ3Y'><ins id='rdZ3Y'></ins><ul id='rdZ3Y'></ul><sub id='rdZ3Y'></sub></form><legend id='rdZ3Y'></legend><bdo id='rdZ3Y'><pre id='rdZ3Y'><center id='rdZ3Y'></center></pre></bdo></b><th id='rdZ3Y'></th></span></q></dt></tr></i><div id='rdZ3Y'><tfoot id='rdZ3Y'></tfoot><dl id='rdZ3Y'><fieldset id='rdZ3Y'></fieldset></dl></div>

      1. <tfoot id='rdZ3Y'></tfoot>

        从 XHR 请求中获取 BLOB 数据

        时间:2023-10-14
      2. <legend id='rDLDg'><style id='rDLDg'><dir id='rDLDg'><q id='rDLDg'></q></dir></style></legend><tfoot id='rDLDg'></tfoot>

              <bdo id='rDLDg'></bdo><ul id='rDLDg'></ul>
                <tbody id='rDLDg'></tbody>

                  <small id='rDLDg'></small><noframes id='rDLDg'>

                1. <i id='rDLDg'><tr id='rDLDg'><dt id='rDLDg'><q id='rDLDg'><span id='rDLDg'><b id='rDLDg'><form id='rDLDg'><ins id='rDLDg'></ins><ul id='rDLDg'></ul><sub id='rDLDg'></sub></form><legend id='rDLDg'></legend><bdo id='rDLDg'><pre id='rDLDg'><center id='rDLDg'></center></pre></bdo></b><th id='rDLDg'></th></span></q></dt></tr></i><div id='rDLDg'><tfoot id='rDLDg'></tfoot><dl id='rDLDg'><fieldset id='rDLDg'></fieldset></dl></div>

                  本文介绍了从 XHR 请求中获取 BLOB 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  var xhr = new XMLHttpRequest();xhr.open('GET', 'http://static.reddit.com/reddit.com.header.png', true);xhr.responseType = 'arraybuffer';xhr.onload = 函数(e){如果(this.status == 200){var uInt8Array = new Uint8Array(this.response);var byte3 = uInt8Array[4];var bb = new WebKitBlobBuilder();bb.append(xhr.response);var blob = bb.getBlob('image/png');var base64 = window.btoa(blob);警报(base64);}};xhr.send();

                  基本上,我在这里要做的是检索图像,并将其转换为 base64.

                  从这里的评论中阅读,它指出:

                  <块引用>

                  当然.在获取资源作为 ArrayBuffer 后,从它.一旦你有了它,你可以直接对文件/blob进行base64编码window.btoa()FileReader.readAsDataURL()."

                  但是,blob 只是 [object blob],而我需要从图像中获取二进制文件,以便将其转换为 base64 并在 img 中显示使用数据标记.

                  有人知道如何实现吗?

                  提前谢谢你!

                  解决方案

                  不要在 Chrome 中使用 BlobBuilder(在 OSX Chrome、Firefox 12、Safari 6、iOS Chrome、iOS Safari 中测试):

                  ex1:http://jsfiddle.net/malraux/xGUsu/(原理)p>

                  ex2:http://jsfiddle.net/xGUsu/78/(使用完整示例)

                  var xhr = new XMLHttpRequest();xhr.open('GET', 'doodle.png', true);xhr.responseType = 'arraybuffer';//当请求准备好时处理响应.xhr.onload = 函数(e){如果(this.status == 200){//从返回的数据创建一个二进制字符串,然后将其编码为数据 URL.var uInt8Array = new Uint8Array(this.response);var i = uInt8Array.length;var binaryString = new Array(i);当我 - ){binaryString[i] = String.fromCharCode(uInt8Array[i]);}var data = binaryString.join('');var base64 = window.btoa(数据);document.getElementById("myImage").src="data:image/png;base64," + base64;}};xhr.send();

                  注意:这段代码现在已经超过 7 年了. 虽然它在大多数浏览器中仍然可以正常工作,但这里是基于 @TypeError 建议的更新版本这只适用于更现代的浏览器iOS Safari 可能例外(可能支持也可能不支持responseType = 'blob' - 确保测试!):

                  var xhr = new XMLHttpRequest();xhr.open('get', 'doodle.png', true);//直接将数据作为 Blob 加载.xhr.responseType = 'blob';xhr.onload = () =>{document.querySelector('#myimage').src = URL.createObjectURL(this.response);};xhr.send();

                  var xhr = new XMLHttpRequest();
                  xhr.open('GET', 'http://static.reddit.com/reddit.com.header.png', true);
                  
                  xhr.responseType = 'arraybuffer';
                  
                  xhr.onload = function(e) {
                    if (this.status == 200) {
                      var uInt8Array = new Uint8Array(this.response);
                      var byte3 = uInt8Array[4]; 
                  
                      var bb = new WebKitBlobBuilder();
                      bb.append(xhr.response);
                      var blob = bb.getBlob('image/png'); 
                      var base64 = window.btoa(blob);
                      alert(base64);
                  
                    }
                  };
                  
                  xhr.send();
                  

                  Basically, what I am trying to do here is retrieve an image, and convert it to base64.

                  From reading in the comments here, it states:

                  "Sure. After fetching a resource as an ArrayBuffer, create a blob from it. Once you have that, you could base64 encode the file/blob directly window.btoa() or FileReader.readAsDataURL()."

                  However, blob is just [object blob], while I need to get the binary from the image so I can convert it to base64 and display it in a img tag using data.

                  Anyone know how to achieve this?

                  Thank you in advance!

                  解决方案

                  Don't use BlobBuilder in Chrome (tested in OSX Chrome, Firefox 12, Safari 6, iOS Chrome, iOS Safari):

                  ex1 : http://jsfiddle.net/malraux/xGUsu/ (principle)

                  ex2: http://jsfiddle.net/xGUsu/78/ (working with full example)

                  var xhr = new XMLHttpRequest();
                  xhr.open('GET', 'doodle.png', true);
                  
                  xhr.responseType = 'arraybuffer';
                  
                  // Process the response when the request is ready.
                  xhr.onload = function(e) {
                    if (this.status == 200) {
                      // Create a binary string from the returned data, then encode it as a data URL.
                      var uInt8Array = new Uint8Array(this.response);
                      var i = uInt8Array.length;
                      var binaryString = new Array(i);
                      while (i--)
                      {
                        binaryString[i] = String.fromCharCode(uInt8Array[i]);
                      }
                      var data = binaryString.join('');
                  
                      var base64 = window.btoa(data);
                  
                      document.getElementById("myImage").src="data:image/png;base64," + base64;
                    }
                  };
                  
                  xhr.send();
                  

                  Note: This code is over 7 years old at this point. While it should still function in most browsers, here's an updated version based on a suggestion by @TypeError that will only work in more modern browsers with the possible exception of iOS Safari (which may or may not support responseType = 'blob' - make sure to test!):

                  var xhr = new XMLHttpRequest();
                  xhr.open('get', 'doodle.png', true);
                  
                  // Load the data directly as a Blob.
                  xhr.responseType = 'blob';
                  
                  xhr.onload = () => {
                    document.querySelector('#myimage').src = URL.createObjectURL(this.response);
                  };
                  
                  xhr.send(); 
                  

                  这篇关于从 XHR 请求中获取 BLOB 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:防止 Xmlhttprequest 的重定向 下一篇:HTTP 状态码 0 是否有任何意义?

                  相关文章

                  最新文章

                  <small id='c8JZ2'></small><noframes id='c8JZ2'>

                    • <bdo id='c8JZ2'></bdo><ul id='c8JZ2'></ul>
                    <legend id='c8JZ2'><style id='c8JZ2'><dir id='c8JZ2'><q id='c8JZ2'></q></dir></style></legend>

                    1. <i id='c8JZ2'><tr id='c8JZ2'><dt id='c8JZ2'><q id='c8JZ2'><span id='c8JZ2'><b id='c8JZ2'><form id='c8JZ2'><ins id='c8JZ2'></ins><ul id='c8JZ2'></ul><sub id='c8JZ2'></sub></form><legend id='c8JZ2'></legend><bdo id='c8JZ2'><pre id='c8JZ2'><center id='c8JZ2'></center></pre></bdo></b><th id='c8JZ2'></th></span></q></dt></tr></i><div id='c8JZ2'><tfoot id='c8JZ2'></tfoot><dl id='c8JZ2'><fieldset id='c8JZ2'></fieldset></dl></div>

                      <tfoot id='c8JZ2'></tfoot>