uni-app hbuilder h5+ webapp安卓 xmlhttprequest请求获取不到arraybuffer类型数据
uni-app hbuilder h5+ webapp安卓 xmlhttprequest请求获取不到arraybuffer类型数据
示例代码:
var xhr = new XMLHttpRequest();
xhr.open('GET', file);
xhr.responseType = 'arraybuffer'; // 设置响应类型
xhr.onload = function() {
console.log(xhr)
if (xhr.status === 200) {
// 处理 arrayBuffer 数据
var arrayBuffer = new Uint8Array(xhr.response); // 或 xhr.responseBody
}
};
xhr.send();
操作步骤:
发起请求
预期结果:
返回arraybuffer数据类型
实际结果:
返回的是字符串
bug描述:
hbuilder h5+ webapp安卓 xmlhttprequest请求获取不到arraybuffer类型数据。
信息类别 | 详情 |
---|---|
产品分类 | HTML5+ |
HBuilderX版本号 | 4.64 |
手机系统 | Android |
手机系统版本号 | Android 10 |
手机厂商 | 华为 |
手机机型 | MatePad Pro |
打包方式 | 离线 |
更多关于uni-app hbuilder h5+ webapp安卓 xmlhttprequest请求获取不到arraybuffer类型数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于uni-app hbuilder h5+ webapp安卓 xmlhttprequest请求获取不到arraybuffer类型数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在uni-app的H5+环境中,Android平台确实存在XMLHttpRequest的responseType设置为’arraybuffer’不生效的问题。这是由于H5+底层实现限制导致的。
解决方案建议:
- 改用uni.request API,它已经封装了对arraybuffer的支持:
uni.request({
url: file,
responseType: 'arraybuffer',
success: (res) => {
const arrayBuffer = new Uint8Array(res.data);
}
});
- 如果必须使用XMLHttpRequest,可以尝试通过base64中转:
xhr.responseType = 'text'; // 改为text
xhr.onload = function() {
const base64Data = xhr.response;
const binaryString = atob(base64Data);
const bytes = new Uint8Array(binaryString.length);
// 转换处理...
};