在实际开发中,会设置到数据需要钱的base64转换,不仅仅是图片才需要base64转换,对于数据也是需要base64转换的
利用window的内置方式来转换
// str 的类型为Object string 均可
var utob=function (str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
var btou=function(str) {
return decodeURIComponent(escape(window.atob(str)));
}
export default {
utob,btou
}
涉及的知识点
- window.btoa 将其转换base64
- window.atob将base64解码
- escape() 来编码字符串
- unescape() 对编码其解码
- encodeURIComponent() 函数可把字符串作为 URI 组件进行编码(说白了 就是对中文的转换)