JavaScript 读取 application/octet-stream 二进制数据并显示
以下是一个示例代码,可以读取 'application/octet-stream' 类型的二进制数据,并将其显示在控制台上:
fetch('https://example.com/some-binary-data', {
headers: {
'Content-Type': 'application/octet-stream'
}
})
.then(response => response.arrayBuffer())
.then(data => {
const byteArray = new Uint8Array(data);
console.log(byteArray);
});
这段代码使用了 fetch API 将请求发送到指定 URL,并设置 Content-Type 头部为 'application/octet-stream'。在得到响应后,它将响应数据转换成一个 ArrayBuffer 对象,然后将其转换为一个 Uint8Array 对象,以便将每个字节显示在控制台上。
原文地址: https://www.cveoy.top/t/topic/m0qq 著作权归作者所有。请勿转载和采集!