ArrayBuffer to base64 encoded string

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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

回答1:

function _arrayBufferToBase64( buffer ) {     var binary = '';     var bytes = new Uint8Array( buffer );     var len = bytes.byteLength;     for (var i = 0; i 

but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6



回答2:

This works fine for me:

var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));

In ES6, the syntax is a little simpler:

let base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));

As pointed out in the comments, this method may result in a runtime error in some browsers when the ArrayBuffer is large. The exact size limit is implementation dependent in any case.



回答3:

For those who like it short, here's an other one using Array.reduce which will not cause stack overflow:

var base64 = btoa(   new Uint8Array(arrayBuffer)     .reduce((data, byte) => data + String.fromCharCode(byte), '') );


回答4:

There is another asynchronous way use Blob and FileReader.

I didn't test the performance. But it is a different way of thinking.

function arrayBufferToBase64( buffer, callback ) {     var blob = new Blob([buffer],{type:'application/octet-binary'});     var reader = new FileReader();     reader.onload = function(evt){         var dataurl = evt.target.result;         callback(dataurl.substr(dataurl.indexOf(',')+1));     };     reader.readAsDataURL(blob); }  //example: var buf = new Uint8Array([11,22,33]); arrayBufferToBase64(buf, console.log.bind(console)); //"CxYh"


回答5:

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.



回答6:

I used this and works for me.

function arrayBufferToBase64( buffer ) {     var binary = '';     var bytes = new Uint8Array( buffer );     var len = bytes.byteLength;     for (var i = 0; i 


回答7:

Below are 2 simple functions for converting Uint8Array to Base64 String and back again

arrayToBase64String(a) {     return btoa(String.fromCharCode(...a)); }  base64StringToArray(s) {     let asciiString = atob(s);     return new Uint8Array([...asciiString].map(char => char.charCodeAt(0))); }


回答8:

You can derive a normal array from the ArrayBuffer by using Array.prototype.slice. Use a function like Array.prototype.map to convert bytes in to characters and join them together to forma string.

function arrayBufferToBase64(ab){      var dView = new Uint8Array(ab);   //Get a byte view              var arr = Array.prototype.slice.call(dView); //Create a normal array              var arr1 = arr.map(function(item){               return String.fromCharCode(item);    //Convert     });      return window.btoa(arr1.join(''));   //Form a string  }

This method is faster since there are no string concatenations running in it.



回答9:

function _arrayBufferToBase64(uarr) {     var strings = [], chunksize = 0xffff;     var len = uarr.length;      for (var i = 0; i * chunksize 

This is better, if you use JSZip for unpack archive from string



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!