JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Source: Internet
Author: User
Tags base64

Always can't remember these two functions, simply write down, better turn.

Avascript Native API was originally supported, Base64, but due to the limitations of the previous JavaScript, resulting in Base64 basic see useless. When the current HTML5 standard is formalized, BASE64 will have a larger transformation space, which can be implemented for HTML5 APIs such as FileReader API, drag-and-drop uploads, and even canvas,video.

Well, before saying a lot, developers need to focus on:

Let's take a look at how to use the Base64 transcoding in JavaScript

' JavaScript '; Window.btoa (str) "amf2yxnjcmlwda=="Window.atob ("amf2yxnjcmlwda==")"JavaScript  " 

Two. For transcoding, the Base64 transcoding object can only be a string, so for other data there is a certain limitation, in particular, it is important to note that Unicode transcoding.

"Window.btoa, China"(str)

Uncaught domexception:failed to execute ' btoa ' in ' Window ': the string to be encoded contains characters outside of the L Atin1 range.

Obviously, this way is not possible, then how to let him support the Chinese character, which will use Window.encodeuricomponent and window.decodeuricomponent

"Chinese, China"; Window.btoa (window.encodeuricomponent (str))//"q2hpbmelruylqkmloemlrtqlqjglquqlrtulouilqkq=" window.decodeuricomponent (window.atob (' q2hpbmelruylqkmloemlrtqlqjglquqlrtulouilqkq= '))//" China, "        

JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.