Convert CSV to JSON client side with React DropZone

前端 未结 1 872
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 16:05

From React dropzone, i receive a File object with a File.preview property whose value is a blob:url. i.e. File {preview: \"blob:http://localhost:8080/52b6bad4-58f4-4ths-a2

相关标签:
1条回答
  • 2021-02-04 17:01

    Yes, its possible with FileReader and csv:

    import csv from 'csv';
    
    // ...
    
    const onDrop = onDrop = (e) => {
        const reader = new FileReader();
        reader.onload = () => {
            csv.parse(reader.result, (err, data) => {
                console.log(data);
            });
        };
    
        reader.readAsBinaryString(e[0]);
    }
    
    // ...
    
    <Dropzone name={field.name} onDrop={onDrop} />
    

    FileReader API: https://developer.mozilla.org/en/docs/Web/API/FileReader
    csv package: https://www.npmjs.com/package/csv

    0 讨论(0)
提交回复
热议问题