-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalFileLoader.ts
31 lines (29 loc) · 1.07 KB
/
LocalFileLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export default class LocalFileLoader {
private request: XMLHttpRequest = new XMLHttpRequest();
public loadAsText(filename: string, onload: (data: string | null) => any): void {
this.request.open('get', filename, true);
this.request.responseType = 'text';
this.request.onload = (ev: Event): any => {
this.request = ev.currentTarget as XMLHttpRequest;
if (this.request) {
onload(this.request.responseText);
} else {
onload(null);
}
};
this.request.send();
}
public loadAsBinary(filename: string, onload: (buffer: ArrayBuffer | null) => any): void {
this.request.open('get', filename, true);
this.request.responseType = 'arraybuffer';
this.request.onload = (ev: Event): any => {
this.request = ev.currentTarget as XMLHttpRequest;
if (this.request) {
onload(this.request.response);
} else {
onload(null);
}
};
this.request.send();
}
}