ArrayBuffer to base64 encoded string

前端 未结 12 1544
渐次进展
渐次进展 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 08:00

    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)));
    }
    

提交回复
热议问题