-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathFile.js
56 lines (46 loc) · 1.2 KB
/
File.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
const {process} = global;
const {EventTarget} = require('./Event');
const {_elementGetter, _elementSetter} = require('./utils');
class FileReader extends EventTarget {
constructor() {
super();
this.result = null;
}
get onload() {
return _elementGetter(this, 'load');
}
set onload(onload) {
_elementSetter(this, 'load', onload);
}
get onerror() {
return _elementGetter(this, 'error');
}
set onerror(onerror) {
_elementSetter(this, 'error', onerror);
}
readAsArrayBuffer(file) {
this.result = file.buffer.buffer.slice(file.buffer.byteOffset, file.buffer.byteOffset + file.buffer.byteLength);
process.nextTick(() => {
this.dispatchEvent(new Event('load', {
target: this,
}));
});
}
readAsDataURL(file) {
this.result = 'data:' + file.type + ';base64,' + file.buffer.toString('base64');
process.nextTick(() => {
this.dispatchEvent(new Event('load', {
target: this,
}));
});
}
readAsText(file) {
this.result = file.buffer.toString('utf8');
process.nextTick(() => {
this.dispatchEvent(new Event('load', {
target: this,
}));
});
}
}
module.exports.FileReader = FileReader;