uni-app 安卓版App atob 解析出的值不正确
uni-app 安卓版App atob 解析出的值不正确
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | win7 | HBuilderX |
示例代码:
function base64ToUint8Array(base64String) { const BASE64_MARKER = ‘;base64,’; const base64Index = base64String.indexOf(BASE64_MARKER) + BASE64_MARKER.length; const base64 = base64String.substring(base64Index); let rawData = Base64.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } console.log(‘outputArray’, outputArray.length) return outputArray.buffer; }
let aa = ‘data:application/dat;base64,EooBCkQIARJACAgQNBoCrwEgACgAMAA4lNwHQiQIAxIgyCazJ1eoSewjpJyWH675MT5aICOnZ8A1y04eULg5XHRIAFIECAESABAAGkCJEmStGfFunnIh94pH/peMTIbrLoluZpVCCw0JQLe+tSOsDYxLJ1u1OnLF/4ZosFDt+H6TKNTOfj3gu0cSAA2a’; let arrayBuffer = base64ToUint8Array(aa); let arr = Array.prototype.slice.call(new Uint8Array(arrayBuffer )); console.log(‘arrarrarrarr’, arr.toString())
在安卓中的到的是 数组\[18,253,1,10,68,8,1,18,64,8,8,16,52,26,2,253,1,32,0,40,0,48,0,56,253,253,7,66,36,8,3,18,32,253,38,253,39,87,253,73,253,35,253,253,253,31,253,253,49,62,90,32,35,253,103,253,53,253,78,30,80,253,57,92,116,72,0,82,4,8,1,18,0,16,0,26,64,253,18,100,253,25,253,110,253,114,33,253,253,71,253,253,253,76,253,253,46,253,110,102,253,66,11,13,9,64,253,253,253,35,253,13,253,75,39,91,253,58,114,253,253,253,104,253,80,253,253,126,253,40,253,253,126,61,253,71,18,0,13,253\]
在ios中的到的 \[ 18,138,1,10,68,8,1,18,64,8,8,16,52,26,2,175,1,32,0,40,0,48,0,56,148,220,7,66,36,8,3,18,32,200,38,179,39,87,168,73,236,35,164,156,150,31,174,249,49,62,90,32,35,167,103,192,53,203,78,30,80,184,57,92,116,72,0,82,4,8,1,18,0,16,0,26,64,137,18,100,173,25,241,110,158,114,33,247,138,71,254,151,140,76,134,235,46,137,110,102,149,66,11,13,9,64,183,190,181,35,172,13,140,75,39,91,181,58,114,197,255,134,104,176,80,237,248,126,147,40,212,206,126,61,224,187,71,18,0,13,154\]
经测试 ios中的值为正确的值
在浏览器中也是ios的值
操作步骤:
直接运行上述方法
# 预期结果:
安卓得正确的值
实际结果:
安卓得不正确的值
# bug描述:
base64 值:data:application/dat;base64,EooBCkQIARJACAgQNBoCrwEgACgAMAA4lNwHQiQIAxIgyCazJ1eoSewjpJyWH675MT5aICOnZ8A1y04eULg5XHRIAFIECAESABAAGkCJEmStGfFunnIh94pH/peMTIbrLoluZpVCCw0JQLe+tSOsDYxLJ1u1OnLF/4ZosFDt+H6TKNTOfj3gu0cSAA2a
在ios中解析出和安卓不同
更多关于uni-app 安卓版App atob 解析出的值不正确的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这里的Base64 哪里来的。
不好意思 这个是定义了个变量 引用了个模块 直接用atob就行了
回复 3***@qq.com: 好
回复 DCloud_Android_DQQ: 有结果吗?
回复 3***@qq.com: 我看看
我这里测试没有复现问题
Android: 荣耀30
IOS: iPhone 7P
对比:
测试代码:
base64ToUint8Array(base64String) {
const BASE64_MARKER = ';base64,';
const base64Index = base64String.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = base64String.substring(base64Index);
let rawData = atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
console.log('outputArray', outputArray.length)
return outputArray.buffer;
},
atobTest:function(){
let arrayBuffer = this.base64ToUint8Array(this.aa);
let arr = Array.prototype.slice.call(new Uint8Array(arrayBuffer));
console.log('arrarrarrarr', arr.toString());
},<br>
我试了下 还是不对 现在不用这个了 换了种方式 用了个模块 暂时可以用了
我这又试了下 还是不对 手机是华为nova 6 se
这是一个在uni-app安卓平台下常见的base64解码问题。问题出在安卓平台对atob
方法的实现与iOS/浏览器不一致。
建议解决方案:
- 使用uni-app提供的
plus.base64.decode
方法替代atob
:
function base64ToUint8Array(base64String) {
const BASE64_MARKER = ';base64,';
const base64Index = base64String.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = base64String.substring(base64Index);
// 使用plus.base64.decode替代atob
const rawData = plus.base64.decode(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray.buffer;
}
- 或者使用第三方base64库如
js-base64
:
import { Base64 } from 'js-base64';
function base64ToUint8Array(base64String) {
const BASE64_MARKER = ';base64,';
const base64Index = base64String.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = base64String.substring(base64Index);
const rawData = Base64.decode(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray.buffer;
}