How to go from Blob to ArrayBuffer

前端 未结 6 1040
旧时难觅i
旧时难觅i 2020-11-27 10:20

I was studying Blobs, and I noticed that when you have an ArrayBuffer, you can easily convert this to a Blob as follows:

var dataView = new DataView(arrayBuf         


        
6条回答
  •  悲&欢浪女
    2020-11-27 10:47

    There is now (Chrome 76+ & FF 69+) a Blob.prototype.arrayBuffer() method which will return a Promise resolving with an ArrayBuffer representing the Blob's data.

    (async () => {
      const blob = new Blob(['hello']);
      const buf = await blob.arrayBuffer();
      console.log( buf.byteLength ); // 5
    })();

提交回复
热议问题