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