admin管理员组

文章数量:1323682

Why is readAsBinaryString() deprecated? From W3C

The use of readAsArrayBuffer() is preferred over readAsBinaryString(), which is provided for backwards patibility.

readAsBinaryString returns a pletely different thing than the other method, so how could one be the replacement for the other?

In my specific case I have a Blob that I need to convert to base64, there are many ways, but most of them not memory efficient. As of my tests calling window.btoa() from readAsBinaryString' result works best. If I cannot use this anymore (or for now lets say "should"), then I must convert the array to a string using iteration and strings concatenation which is not memory efficient at all!

So after researching for days I don't really find an alternative to readAsBinaryString, that's why the question, or do you see an alternative that also works with 100MB blobs?

Why is readAsBinaryString() deprecated? From W3C

The use of readAsArrayBuffer() is preferred over readAsBinaryString(), which is provided for backwards patibility.

readAsBinaryString returns a pletely different thing than the other method, so how could one be the replacement for the other?

In my specific case I have a Blob that I need to convert to base64, there are many ways, but most of them not memory efficient. As of my tests calling window.btoa() from readAsBinaryString' result works best. If I cannot use this anymore (or for now lets say "should"), then I must convert the array to a string using iteration and strings concatenation which is not memory efficient at all!

So after researching for days I don't really find an alternative to readAsBinaryString, that's why the question, or do you see an alternative that also works with 100MB blobs?

Share edited Apr 24, 2019 at 18:54 David asked Apr 24, 2019 at 18:52 DavidDavid 4,8372 gold badges37 silver badges77 bronze badges 10
  • 6 You're misunderstanding the difference between deprecated and preferred use – Sterling Archer Commented Apr 24, 2019 at 18:54
  • But they say they removed the function first and then added it again only for backward patibility, so this means they will remove it one day. – David Commented Apr 24, 2019 at 18:55
  • "While a deprecated software feature remains in the software, its use may raise warning messages remending alternative practices; deprecated status may also indicate the feature will be removed in the future. Features are deprecated rather than immediately removed, to provide backward patibility, and to give programmers time to bring affected code into pliance with the new standard." -- Wikipedia – user47589 Commented Apr 24, 2019 at 18:57
  • Lots of methods will be deprecated one day, you simply adapt when it happens :) As of right now, unless you want to adjust your code to use ArrayBuffer, it seems like a null issue – Sterling Archer Commented Apr 24, 2019 at 18:57
  • A little research reveals lists.w3/Archives/Public/public-webapps/2011OctDec/… – user1228 Commented Apr 24, 2019 at 18:57
 |  Show 5 more ments

1 Answer 1

Reset to default 7

The history is that readAsBinaryString was present in an early specification of the FileReader API, before the ArrayBuffer interface exist.

When the ArrayBuffer interface appeared, readAsBinaryString has been deprecated because all its use cases could be done in a better way using that new interface.
Indeed, readAsBinaryString only converts the binary data into a DOMString (UTF-16). There is not much you can do from it afterward. Also, storing it as an UTF-16 string implies that it takes far more space in memory than the original data size. Add to this that strings are immutable, I guess you can see how inefficient it is to work from this.
And finally, if you really need to have this string, you can actually do the same from an ArrayBuffer, you just need to call String.fromCharCode over an Uint8 view of this ArrayBuffer.

// generate some binary data
document.createElement('canvas').toBlob(blob => {
  const bin_reader = new FileReader();
  const arr_reader = new FileReader();
  let done = 0;
  bin_reader.onload = arr_reader.onload = e => {
    if(++done===2) {
      const arr_as_bin = [...new Uint8Array(arr_reader.result)]
        .map(v => String.fromCharCode(v)).join('');
      console.log('same results: ', arr_as_bin === bin_reader.result);
      console.log(arr_as_bin);
    }
  }
  bin_reader.readAsBinaryString(blob);
  arr_reader.readAsArrayBuffer(blob);
});

Now, this method, while still very useless, has been re-added to the specs, because some websites did start using it.

And to help OP a bit more, since what they were trying to do was actually to get a base64 version of their Blob, then don't even use readAsArrayBuffer(), readAsDataURL() is what you need:

const blob = new Blob(['hello']);
const reader = new FileReader();
reader.onload = e => {
  const dataURL = reader.result;
  const base64 = dataURL.slice(dataURL.indexOf(',')+1);
  console.log(base64);
};
reader.readAsDataURL(blob);

本文标签: javascriptWhy is readAsBinaryString() deprecatedStack Overflow