-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.js
36 lines (33 loc) · 887 Bytes
/
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
export let filename = 'new_file.txt';
export function save(text) {
const link = document.createElement('a');
link.setAttribute(
'href',
'data:text/plain;charset=utf-8,'
+ encodeURIComponent(text)
);
link.setAttribute('download', filename);
if (document.createEvent) {
var evnt = document.createEvent('MouseEvents');
evnt.initEvent('click', true, true);
link.dispatchEvent(evnt);
}
else {
link.click();
}
}
export async function load() {
return new Promise((resolve, reject) => {
const el = document.createElement('input');
el.type = 'file';
el.addEventListener('change', function() {
var fr=new FileReader();
fr.onload=function() {
resolve(fr.result)
filename = el.files[0].name;
}
fr.readAsText(this.files[0]);
})
el.click();
})
}