File size: 1,103 Bytes
6bcb42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Papa from 'papaparse';

export default () => new Promise((resolve, reject) => {
    const fileInput = document.createElement('input');
    fileInput.setAttribute('type', 'file');
    fileInput.setAttribute('accept', '.csv, .tsv, .txt'); // parser auto-detects delimiter
    fileInput.onchange = e => {
        const file = e.target.files[0];
        const fr = new FileReader();
        fr.onload = () => {
            document.body.removeChild(fileInput);
            const text = fr.result;
            Papa.parse(text, {
                header: false,
                complete: results => {
                    resolve({
                        rows: results.data,
                        text
                    });
                },
                error: err => {
                    reject(err);
                }
            });
        };
        fr.onerror = () => {
            document.body.removeChild(fileInput);
            reject(new Error('Cannot read file'));
        };
        fr.readAsText(file);
    };
    document.body.appendChild(fileInput);
    fileInput.click();
});