我正在使用范围请求从一个大的二进制文件中读取许多32位浮点数,不幸的是,我从这个文件中需要的浮点数位于文件的不同部分,因此我需要用多个范围进行范围请求。
fetch("http://example.com/largeBinaryFile.bin", {
headers: {
'content-type': 'multipart/byteranges',
'range': 'bytes=2-5,10-13',
},
})
.then(response => {
if (response.ok) {
return response.text();
}
})
.then(response => {
console.log(response);
});
由于多个范围,我必须使用文本而不是arrayBuffer,当我打印出响应时
--00000000000000000002
Content-Type: application/octet-stream
Content-Range: bytes 2-5/508687874
1ȹC
--00000000000000000002
Content-Type: application/octet-stream
Content-Range: bytes 10-13/508687874
þC
--00000000000000000002--
正如您所看到的,二进制数据在不同的部分,我尝试使用Blob和FileReader将数据转换为arrayBuffer,并使用Float32Array包装,但没有成功。我可以问如何从多个部分获得32位浮点值吗?提前谢谢你的帮助。
发布于 2022-08-20 08:11:21
嗨,我是弗兰克,铬工程师,这里有一些深层次的内部,我使用我的https://github.com/stealify/stealify项目二进制互操作铬。
const charset = 'x-user-defined';
// Maps to the UTF Private Address Space Area so you can get bits as chars
const binaryRawEnablingHeader = `text/plain; charset=${charset}`;
// UNICODE Private Area 0xF700-0xF7ff.
const convertToAbyte = (chars) =>
new Array(chars.length)
.map((_abyte,offset) => chars.charCodeAt(offset) & 0xff);
const _BinaryRawFetch = (url) => fetch(url,{ headers: {
'Content-Type': binaryRawEnablingHeader,
'range': 'bytes=2-5,10-13'
}}).then(
(res) => convertToAbyte(res.text())
);
要将其转换为文本结果,您需要将正确的complet部分分割出来,您知道每个部分的大小,然后像以前一样使用codePointAt处理它,但只有.map(String.codePointAt) codePointAt才能将其转换为有效的UTF 8字符。如果需要的话。我只为他的JSON的评论写了这篇文章。
https://stackoverflow.com/questions/45059350
复制相似问题