ArrayBuffer to blob conversion

前端 未结 2 499
暗喜
暗喜 2020-12-01 07:50

I have a project where I need to display djvu schemas in browser.

I found this old library on Github which, as far as I understood, converts djvu files to bmp and th

相关标签:
2条回答
  • 2020-12-01 08:07
    var buffer = new ArrayBuffer(32);
    
    new Blob([buffer]);
    

    so the Uint8Array should be

    new Blob([new Uint8Array([1, 2, 3, 4]).buffer]);
    
    0 讨论(0)
  • 2020-12-01 08:18

    I don't know why the author did wrap his Uint8Array in an new one... note that I don't really know either the deprecated BlobBuilder API, but one typo I can see in your code is that you need to wrap your TypedArray in a normal Array:

    new Blob([new Uint8Array(buffer, byteOffset, length)]);
    

    The Blob constructor takes a blobParts sequence as first parameter, and then searches for BufferSource, USVStrings and Blob elements in this sequence. So when you pass a TypedArray, it will actually iterate over all the entries of this TypedArray and treat these as USVString (and thus convert their numerical value to UTF-8 strings in the Blob). That's rarely what you want, so better always pass an Array in this constructor.

    0 讨论(0)
提交回复
热议问题