使用React DropZone在客户端将CSV转换为JSON

8

React dropzone 中,我收到一个带有File.preview属性的File对象,其值是一个blob:url。例如:File {preview: "blob:http://localhost:8080/52b6bad4-58f4-4ths-a2f5-4ee258ba864a"

有没有办法在客户端将其转换为JSON?这个文件不需要被存储在数据库中(转换后的JSON需要)。我尝试使用csvtojson,但它无法使用文件系统,因为它使用Node来支持它。如果可能的话,最好能在客户端上传后进行转换。欢迎任何建议。

        <Dropzone
            name={field.name}
            onDrop={(acceptedFiles, rejectedFiles) => {
                acceptedFiles.forEach(file => {
                    console.log(file)
                    let tempFile = file.preview
                    csv()
                        .fromSteam(tempFile) // this errors with fs.exists not a function as its not running serverside

                        .on('end_parsed',(jsonArrObj)=>{
                            console.log(jsonArrObj)
                        })
                })
            }}
        >
1个回答

18

在这里将数组转换为对象的数组:https://dev59.com/CGIi5IYBdhLWcg3w8AFy - buncis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接