How can you encode a string to Base64 in JavaScript?

前端 未结 26 3785
梦如初夏
梦如初夏 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 05:00

    You can use btoa (to base-64) and atob (from base-64).

    For IE 9 and below, try the jquery-base64 plugin:

    $.base64.encode("this is a test");
    $.base64.decode("dGhpcyBpcyBhIHRlc3Q=");
    

提交回复
热议问题