Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created August 1, 2022 02:16
Show Gist options
  • Save semlinker/d5eeb5d6e22f97b01a9f205457bc80d3 to your computer and use it in GitHub Desktop.
Save semlinker/d5eeb5d6e22f97b01a9f205457bc80d3 to your computer and use it in GitHub Desktop.
HTTP Transfer Large Files
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Range Request Demo (Bytefer)</title>
</head>
<body>
<h3>Range Request Demo (Bytefer)</h3>
<div id="msgList"></div>
<script>
const msgList = document.querySelector("#msgList");
function getBinaryContent(url, start, end, responseType = "arraybuffer") {
return new Promise((resolve, reject) => {
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("range", `bytes=${start}-${end}`);
xhr.responseType = responseType;
xhr.onload = function () {
resolve(xhr.response);
};
xhr.send();
} catch (err) {
reject(new Error(err));
}
});
}
getBinaryContent(
"http://localhost:3000/big-file.txt",
0,
100,
"text"
).then((text) => {
msgList.append(`${text}`);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment