How to go from Blob to ArrayBuffer

前端 未结 6 1042
旧时难觅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:53

    The Response API consumes a (immutable) Blob from which the data can be retrieved in several ways. The OP only asked for ArrayBuffer, and here's a demonstration of it.

    var blob = GetABlobSomehow();
    
    // NOTE: you will need to wrap this up in a async block first.
    /* Use the await keyword to wait for the Promise to resolve */
    await new Response(blob).arrayBuffer();   //=> 
    

    alternatively you could use this:

    new Response(blob).arrayBuffer()
    .then(/*  */);
    

    Note: This API isn't compatible with older (ancient) browsers so take a look to the Browser Compatibility Table to be on the safe side ;)

提交回复
热议问题