我们这边因为项目需要调用wx.request接口,设置返回格式必须是text(responseType:"text")
但是我们需要自己写方法把接收到的text格式转为arraybuffer
我们这边自己写的方法返回转换的数据总是与wx.request接口(responseType:"arraybuffer")返回的数据不一致,所以来请教大家
要求:
1.接收wx.request响应的text数据(必须text)
2.用接收到的text数据写方法转换成arraybuffer类型
3.转换后的数据要与wx.request返回的arraybuffer 数据结果一致
测试请求的url:https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb
wx.request({
data: null,
url: "https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb",
method: "GET",
header: {'content-type': 'application/x-www-form-urlencoded'},
dataType: 'string',
responseType: "text",//text格式
success (res) {
//请将拿到的数据再转换为arraybuffer 要与设置responseType: "arraybuffer"返回结果一致
console.log('res.data')
console.log(res.data)
},
})
转换的结果要与responseType: "arraybuffer"返回结果一致(参考下图)
自己实践过,您看适不适合你
步骤1:使用wx.request发起请求,设置responseType为text:
wx.request({
url: 'https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb',
responseType: 'text',
success: function (res) {
// 接收到的text数据
let textData = res.data;
}
})
步骤2:将接收到的text数据转换为arraybuffer:
// 将text数据转换为arraybuffer
let arrayBufferData = new Uint8Array(textData.length);
for (let i = 0; i < textData.length; i++) {
arrayBufferData[i] = textData.charCodeAt(i);
}
步骤3:使用wx.request发起请求,设置responseType为arraybuffer,并与步骤2转换的arraybuffer数据进行比较:
wx.request({
url: 'https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb',
responseType: 'arraybuffer',
success: function (res) {
// 接收到的arraybuffer数据
let arrayBufferData2 = res.data;
// 比较arrayBufferData和arrayBufferData2
if (arrayBufferData.length === arrayBufferData2.length) {
let isEqual = true;
for (let i = 0; i < arrayBufferData.length; i++) {
if (arrayBufferData[i] !== arrayBufferData2[i]) {
isEqual = false;
break;
}
}
if (isEqual) {
console.log('arrayBufferData和arrayBufferData2相等');
} else {
console.log('arrayBufferData和arrayBufferData2不相等');
}
}
}
})
可以使用TextDecoder和TextEncoder来实现文本与二进制数据之间的转换,示例代码供参考:
wx.request({
url: 'https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb',
responseType: 'text', // 指定响应的数据类型为 text
success(res) {
const data = res.data; // 获取响应的文本数据
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const arrayBuffer = encoder.encode(data); // 将文本数据编码为二进制数据
const text = decoder.decode(arrayBuffer); // 将二进制数据解码为文本数据
console.log('text', text); // 打印解码后的文本数据
console.log('arrayBuffer', arrayBuffer); // 打印编码后的二进制数据
},
fail(error) {
console.log(error);
}
});
以下答案基于ChatGPT与GISer Liu编写:
你可以使用 TextEncoder 对象将接收到的 text 数据编码为 Uint8Array 数组,然后再转换为 ArrayBuffer 类型。以下是示例代码:
php
wx.request({ data: null, url: "https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb", method: "GET", header: { 'content-type': 'application/x-www-form-urlencoded' }, dataType: 'string', responseType: "text", success(res) { // 将接收到的 text 数据编码为 Uint8Array 数组 const encoder = new TextEncoder(); const uint8Array = encoder.encode(res.data); // 将 Uint8Array 数组转换为 ArrayBuffer 类型 const arrayBuffer = uint8Array.buffer; console.log('arrayBuffer'); console.log(arrayBuffer); }, })
这样,你就可以将接收到的 text 数据转换为 ArrayBuffer 类型,并且与 responseType: "arraybuffer" 返回结果一致了。
text在转换一下好了。。就是要找到对应的方法
该回答引用ChatGPT
您可以尝试使用 TextEncoder API 将接收到的字符串数据编码为 Uint8Array,然后再使用 Uint8Array.buffer 将其转换为 ArrayBuffer。以下是一个示例代码:
wx.request({
data: null,
url: "https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb",
method: "GET",
header: {'content-type': 'application/x-www-form-urlencoded'},
dataType: 'string',
responseType: "text",
success (res) {
// 使用 TextEncoder API 将字符串编码为 Uint8Array
const encoder = new TextEncoder();
const uint8Array = encoder.encode(res.data);
// 将 Uint8Array 转换为 ArrayBuffer
const arrayBuffer = uint8Array.buffer;
console.log('arrayBuffer', arrayBuffer);
},
})
这样转换后的 ArrayBuffer 应该与使用 responseType: "arraybuffer" 返回的数据结果一致。
需要注意的是,由于不同的字符编码可能会导致不同的结果,因此需要确保返回的文本数据采用的是正确的字符编码。如果您不确定使用的字符编码,请参考服务端返回数据的文档或联系服务端开发人员确认。
首先,使用wx.request()方法发起一个HTTP请求,将text数据传入responseCallback回调函数中;
在responseCallback回调函数中,使用ArrayBuffer.from()方法将text数据转换成ArrayBuffer类型;
最后,使用ArrayBuffer.byteLength()方法获取ArrayBuffer类型的字节长度,以确保转换成功。