且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ArrayBuffer为Base64 EN codeD字符串

更新时间:2023-11-28 23:42:58

 函数_arrayBufferToBase64(缓冲){
    VAR二进制='';
    VAR字节=新Uint8Array(缓冲);
    VAR LEN = bytes.byteLength;
    对于(VAR I = 0; I< LEN,我++){
        二进制+ = String.fromChar code(字节[I]);
    }
    返回window.btoa(二进制);
}

不过,非本地实现更快例如 https://gist.github.com/958841
看http://jsperf.com/encoding-xhr-image-data/6

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

function _arrayBufferToBase64( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return window.btoa( binary );
}

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