前端多媒体(2)—— xhr异步接收处理二进制数据

有时咱们须要把远程的视频、图片数据异步下载下来,而后在js里进行特殊处理。好比把VR的图片特殊处理,把不一样封装格式的视频作一次 转封装 处理等等,这类操做都要先获取二进制数据,而后特殊处理。javascript

这个时候就须要用到 XMLHttpRequest 2.0 的 responseType 属性了。XMLHttpRequest.responseType 属性是一个枚举值,经过它能够指定返回响应的类型。html

常见的类型有java

  • arraybuffer: 二进制数据
  • blob:二进制大对象,好比图片、视频
  • document: xml数据类型
  • json:返回数据会用被解析为JSON
  • text (文本形式)

这里 ArrayBuffer 和 Blob 都是二进制数据,可是二者是不一样的,ArrayBuffer和Blob建立后不能修改,除非新建另外一个,可是 Blob 对象能够指定创MINE类型,ArrayBuffer能够做为Blob构造器的参数传。git

readyState

状态 描述
0 UNSENT (未打开) open()方法还未被调用.
1 OPENED (未发送) send()方法还未被调用.
2 HEADERS_RECEIVED (已获取响应头) send()方法已经被调用, 响应头和响应状态已经返回.
3 LOADING (正在下载响应体) 响应体下载中; responseText中已经获取了部分数据.
4 DONE (请求完成) 整个请求过程已经完毕.

responseType = 'text'

这种状况下返回的数据会以文本形式得到,而且xhr.responseText这个属性里得到数据github

var xhr = new XMLHttpRequest();

xhr.open('GET', './assets/text');
xhr.responseType = 'text';
xhr.send();
xhr.onreadystatechange = function(){
  if (xhr.readyState == 4 && xhr.status == 200){
    alert(xhr.responseText);
  }
}

点击这里查看demo https://young-cowboy.github.io/gallery/XMLHttpRequest_responseType/response_document.htmljson

responseType = 'document'

在这种事状况下了浏览器将返回的数据解析为xml形式。可是要注意http的response header 的 Content-Type:text/xml 不然浏览器没法识别这种数据格式。而后得到的数据会在 xhr.responseXML 里。数组

var xhr = new XMLHttpRequest();

xhr.open('GET', './assets/document.xml');
xhr.responseType = 'document';
xhr.send();
xhr.onreadystatechange = function(){
  if (xhr.readyState == 4 && xhr.status == 200){
    alert(xhr.response.getElementById('name').textContent)
  }
}

请求的数据为浏览器

<?xml version="1.0" encoding="utf-8"?>
<document>
  <name id="name">young cowboy</name>
</document>

点击这里查看demo https://young-cowboy.github.io/gallery/XMLHttpRequest_responseType/response_document.htmldom

responseType = 'json'

这种状况下返回的数据会以json体现。数据会在xhr.response字段中。异步

var xhr = new XMLHttpRequest();

xhr.open('GET', './assets/json');
xhr.responseType = 'json';
xhr.send();
xhr.onreadystatechange = function(){
  if (xhr.readyState == 4 && xhr.status == 200){
    alert(JSON.stringify(xhr.response, null, 2))
  }
}

json

{"name": "yuoung cowboy"}

注意若是返回的数据不合法的话。是没法获取数据的。

demo点击这里https://young-cowboy.github.io/gallery/XMLHttpRequest_responseType/response_json.html

responseType = 'blob'

Blob这个概念是通用的计算机概念指的是大块的二进制数据,好比视频、图片。更多的例子和讲解会在后续的文章中体现。

在demo中我使用了一张图片,而后把它保存为一张没有扩展名的文件。

<img id="image">
<script type="text/javascript">
  var xhr = new XMLHttpRequest();

  xhr.open('GET', './assets/blob');
  xhr.responseType = 'blob';
  xhr.send();
  xhr.onreadystatechange = function(){
    if (xhr.readyState == 4 && xhr.status == 200){
      console.log(xhr.response instanceof Blob); // true
      document.getElementById('image').src = URL.createObjectURL(xhr.response); // 这里设置一个张图片
    }
  }
</script>

注意,这里使用了一个API,URL.createObjectURL。它能够建立一个连接指向内存中的Blob对象。

demo 能够点击这里 https://young-cowboy.github.io/gallery/XMLHttpRequest_responseType/response_blob.html

responseType = 'arraybuffer'

ArrayBuffer 表示二进制数据的原始缓冲区,他指向内存中的一段数据。它的大小,一般是固定的,也就是在你初始化的时候就决定了。ArrayBuffer自己是不能读写的,须要借助类型化数组或DataView对象来解释原始缓冲区。

Typed Arrays JavaScript中新出现的一个概念,专为访问原始的二进制数据而生。更多关于ArrayBuffer 、 Blob等等处理二进制数据的方法的资料,请关注后续的博客。这里再也不展开讲解。

仍是和以前同样使用一张图片的二进制数据。只不过在返回的数据类型中,设置为arraybuffer。而后再说没有对数据进行作一次包装。

<img id="image">
<script type="text/javascript">
  var xhr = new XMLHttpRequest();

  xhr.open('GET', './assets/arraybuffer');
  xhr.responseType = 'arraybuffer';
  xhr.send();
  xhr.onreadystatechange = function(){
    if (xhr.readyState == 4 && xhr.status == 200){
      var blob = new Blob([xhr.response], {type: 'image/png'});

      document.getElementById('image').src = URL.createObjectURL(blob); // 这里设置一个张图片
    }
  }
</script>

demo https://young-cowboy.github.io/gallery/XMLHttpRequest_responseType/response_arraybuffer.html

参考资料

相关文章
相关标签/搜索