Created
August 1, 2022 02:16
-
-
Save semlinker/d5eeb5d6e22f97b01a9f205457bc80d3 to your computer and use it in GitHub Desktop.
HTTP Transfer Large Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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