mirror of
https://codeberg.org/demostf/frontend.git
synced 2026-06-03 18:24:12 +02:00
some basic viewer
This commit is contained in:
parent
2dee28d022
commit
5910b2f35a
45 changed files with 1089 additions and 1436 deletions
28
script/download.ts
Normal file
28
script/download.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
export async function download(url: string, progress: (number) => void): Promise<ArrayBuffer> {
|
||||
const response = await fetch(url, {mode: 'cors'});
|
||||
|
||||
if (!response.body || !response.headers) {
|
||||
throw new Error("invalid response");
|
||||
}
|
||||
const contentLength = +(response.headers.get('Content-Length') || 0);
|
||||
let receivedLength = 0;
|
||||
|
||||
let data = new Uint8Array(contentLength);
|
||||
|
||||
const reader = response.body.getReader();
|
||||
|
||||
while(true) {
|
||||
const {done, value} = await reader.read();
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
data.set(value, receivedLength);
|
||||
receivedLength += value.length;
|
||||
|
||||
progress((receivedLength / contentLength) * 100);
|
||||
}
|
||||
|
||||
return data.buffer;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue