How can you encode a string to Base64 in JavaScript?

前端 未结 26 3850
梦如初夏
梦如初夏 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:49

    I have re-wrote by hand, these encoding and decoding methods with the exception of the hexadecimal one into a modular format for cross-platform / browser compatibility and also with real private scoping, and uses btoa and atob if they exist due to speed rather than utilize its own encoding:

    https://gist.github.com/Nijikokun/5192472

    Usage:

    base64.encode(/* String */);
    base64.decode(/* String */);
    
    utf8.encode(/* String */);
    utf8.decode(/* String */);
    

提交回复
热议问题