Load and save

This commit is contained in:
Sebastian Seedorf
2022-08-12 21:02:01 +02:00
parent 82888a5450
commit 185f39cb8a
5 changed files with 102 additions and 27 deletions

29
src/download.ts Normal file
View File

@@ -0,0 +1,29 @@
export function download(filename: string, data: Uint8Array) {
const tag = document.createElement("a");
tag.style.display = "none";
document.body.appendChild(tag);
const blob = new Blob([data], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
tag.href = url;
tag.download = filename;
tag.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(tag);
}
export async function streamToArrayBuffer(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
let result = new Uint8Array(0);
const reader = stream.getReader();
while (true) { // eslint-disable-line no-constant-condition
const { done, value } = await reader.read();
if (done) {
break;
}
const newResult = new Uint8Array(result.length + value.length);
newResult.set(result);
newResult.set(value, result.length);
result = newResult
}
return result;
}