How can you encode a string to Base64 in JavaScript?

前端 未结 26 3779
梦如初夏
梦如初夏 2020-11-21 04:02

I have a PHP script that can encode a PNG image to a Base64 string.

I\'d like to do the same thing using JavaScript. I know how to open files, but I\'m not sure how

26条回答
  •  难免孤独
    2020-11-21 04:56

    Please note that this is not suitable for raw Unicode strings! See Unicode section here.

    Syntax for encoding

    var encodedData = window.btoa(stringToEncode);

    Syntax for decoding

    var decodedData = window.atob(encodedData);

提交回复
热议问题