-
Notifications
You must be signed in to change notification settings - Fork 11
/
fileLoader.js
86 lines (63 loc) · 1.54 KB
/
fileLoader.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function FileLoader()
{
this.loadedCount = 0;
this.totalCount = 0;
var self = this;
// PUBLIC //
/// ----------------------
/// load data from URL
this.load = function(url, onSuccessEvent)
{
self.totalCount++;
console.log("Load file: "+ url);
var loader = new ItemLoader(self);
loader.loadThis(url, onSuccessEvent);
}
// PRIVATE //
function ItemLoader(globalFileLoader)
{
this.fl = globalFileLoader;
var self = this;
this.loadThis = function(url, onSuccessEvent)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob";
xhr.onreadystatechange = function ()
{
// if request finished and response is ready
if (xhr.readyState == 4)
{
/// convert blob to 8Bit Array
var fileReader = new FileReader();
fileReader.onload = function()
{
arrayBuffer = this.result;
data = new Uint8Array(arrayBuffer);
fr = new DataReader(data);
fr.filename = url;
//- OK, done!
if (onSuccessEvent)
{
onSuccessEvent(fr);
}
};
fileReader.readAsArrayBuffer(xhr.response);
self.fl._fileLoaded();
}
}
xhr.send(null);
}
}
/// ----------------------
/// load data from URL
this._fileLoaded = function()
{
self.loadedCount++;
if(self.onload)
{
self.onload();
self.onload = undefined;
}
}
}