在过程中,发现报错如下:
failed to execute 'atob' on 'window': the string to be decoded is not correctly encoded.
后来在看atob和btoa的demo时发现base64字符串仅仅是后面的乱七八糟字符串好吗,没有前面的名片。
var str = "Hello World!";
var enc = window.btoa(str);
var dec = window.atob(enc);
var res = "Encoded String: " + enc + "
" + "Decoded String: " + dec;
The result of res will be:
Encoded String: SGVsbG8gV29ybGQh //看这里!!!!
Decoded String: Hello World!
而图片的base64是这样的:
data:image/gif;base64,/9j/4AAQSkZJRgABAQ........
所以只要后面的就可以了:
b64DataStr.substring(b64DataStr.indexOf(',') + 1)
本文探讨了在编码过程中遇到的'atob'解码错误,指出问题在于忽略Base64字符串前的数据标识部分。通过分析'HelloWorld!'的Base64编码与解码示例,理解了Base64编码的基本用法。同时,对比了图片Base64编码的特点,强调在处理图片时只需关注字符串尾部的Base64数据。并提供了正确提取图片Base64数据的方法。
1万+

被折叠的 条评论
为什么被折叠?



