JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Source: Internet
Author: User
Tags base64

JavaScript native APIs were originally supported, Base64, but because of the limitations of the previous JavaScript, Base64 was largely 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

var str = ' javascript '; Window.btoa (str)/ transcoding result "amf2yxnjcmlwda=="  Window.atob ("amf2yxnjcmlwda==")// decoding result "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.

var str = "China, Chinese"window.btoa (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

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

Window.btoa (' {"Success": True, "MSG": null, "ErrorCode": 0, "data": {"MemberID": "6976462", "Canbxmoney": 0, "Bxdasset" : 3000.00, "Bxdprofit": 95.752732}} ');" Eyjzdwnjzxnzijp0cnvllcjtc2ciom51bgwsimvycm9yq29kzsi6mcwizgf0ysi6eyjtzw1izxjjzci6ijy5nzy0njiilcjjyw5cee1vbmv5ijowlcjiegrbc 3nldci6mzawmc4wmcwiynhkuhjvzml0ijo5ns43nti3mzj9fq== "Window.atob (" Eyjzdwnjzxnzijp0cnvllcjtc2ciom51bgwsimvycm9yq29kzsi6mcwizgf0ysi6eyjtzw1izxjjzci6ijy5nzy0njiilcjjyw5cee1vbmv5ijowlcjiegrbc 3nldci6mzawmc4wmcwiynhkuhjvzml0ijo5ns43nti3mzj9fq== ")" {"Success": True, "MSG": null, "ErrorCode": 0, "Data" : {"MemberID": "6976462", "Canbxmoney": 0, "Bxdasset": 3000.00, "Bxdprofit": 95.752732}} "

JavaScript uses Btoa and Atob for Base64 transcoding and decoding

Related Article

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.