UNPKG

698 BJavaScriptView Raw
1export const downloadData2File = (data, MIME = { type: 'application/json' }, ext = 'json') => {
2 const blob = new Blob([JSON.stringify(data)], MIME),
3 fileName = `${new Date().valueOf()}.${ext}`,
4 link = document.createElement('a');
5
6 link.href = window.URL.createObjectURL(blob);
7 link.download = fileName;
8 link.click();
9 window.URL.revokeObjectURL(link.href);
10}
11
12export const getJsonData = file => new Promise((resolve, reject) => {
13 try {
14 const r = new FileReader();
15 r.readAsText(file);
16 r.onload = function ({ target: { result } }) {
17 resolve(JSON.parse(result));
18 }
19 } catch (error) {
20 reject(error);
21 }
22})
\No newline at end of file