遇坑
在使用RN开发下一栈应用时,遇到了一个问题:无法使用fetch
返回数据的arrayBuffer
、blob
数据类型解析。
如图,只有json
、text
、formData
解析方法:

需求
我要请求一个网络图片,然后获取到它的二进制数据。
当然base64
格式的数据也行,因为图片也可以使用data:image/png;base64,xxx
等格式进行转换。
实现
测试了一番,彻底放弃fetch
请求API,改用XMLHttpRequest
进行数据请求。
var xmlRequest = new XMLHttpRequest();
xmlRequest.open("GET", 'https://nextstack.xyz/static/qrcode.png', true);
xmlRequest.responseType = "blob";//这里是关键,它指明返回的数据的类型是二进制
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
console.log(this._response)
}
}
xmlRequest.send(null);
使用this._response
即可直接获取base64
格式的返回数据:

网友评论