ArrayBuffer to base64 encoded string

前端 未结 12 1326
渐次进展
渐次进展 2020-11-22 07:16

I need an efficient (read native) way to convert an ArrayBuffer to a base64 string which needs to be used on a multipart post.

12条回答
  •  太阳男子
    2020-11-22 07:59

    My recommendation for this is to NOT use native btoa strategies—as they don't correctly encode all ArrayBuffer's…

    rewrite the DOMs atob() and btoa()

    Since DOMStrings are 16-bit-encoded strings, in most browsers calling window.btoa on a Unicode string will cause a Character Out Of Range exception if a character exceeds the range of a 8-bit ASCII-encoded character.

    While I have never encountered this exact error, I have found that many of the ArrayBuffer's I have tried to encode have encoded incorrectly.

    I would either use MDN recommendation or gist.

    • https://github.com/beatgammit/base64-js
    • https://gist.github.com/jonleighton/958841

提交回复
热议问题