ArrayBuffer to base64 encoded string

前端 未结 12 1582
渐次进展
渐次进展 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:16

    The OP did not specify the Running Enviroment but if you are using Node.JS there is a very simple way to do such thing.

    Accordig with the official Node.JS docs https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings

    // This step is only necessary if you don't already have a Buffer Object
    const buffer = Buffer.from(yourArrayBuffer);
    
    const base64String = buffer.toString('base64');
    

    Also, If you are running under Angular for example, the Buffer Class will also be made available in a Browser Environment.

提交回复
热议问题