我想知道如何读取从某个URL提取的二进制文件,比方说,我从URL提取了一个文件,例如ABC.bin,现在我想读取它在javascript中的内容,但是我不知道知道如何阅读。我需要阅读内容,然后通过BLE将其发送到另一台设备。有人可以帮助我如何读取文件吗?
您可以使用fetch
从外部源加载文件
fetch("mysite.com/binaries/ABC.bin")
.then(res => res.text())
.then(text =>
console.log(text);
})
.catch(err => {
alert('Something went wrong!')
});
或者,如果您想与旧版浏览器兼容,则可以使用XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.nu.nl/rss/Algemeen");
xhr.onload = function (x) {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
}
xhr.send();