Load and save
This commit is contained in:
29
src/download.ts
Normal file
29
src/download.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user