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

编程入门 行业动态 更新时间:2024-10-28 00:26:05
本文介绍了使用React DropZone将CSV转换为JSON客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能吗?

从反应放置区,我收到了一个File对象,该对象的File.preview属性值为blob:url.即File {preview: "blob:localhost:8080/52b6bad4-58f4-4ths-a2f5-4ee258ba864a"

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

是否可以在客户端上将其转换为json?该文件不需要存储在数据库中(将转换为JSON).我尝试使用 csvtojson ,但由于使用节点供电,因此无法使用文件系统它.理想情况下,一旦用户上载,便希望在客户端中进行转换.任何建议欢迎.

Is there a way to convert this to json on the client? The file isnt need to be stored in a database (the convert JSON will be). I've attempted to use csvtojson but it's unable to use the file system as it uses node to power it. Ideally would like to convert this in the client if possible, once user has uploaded it. Any suggestions welcomed.

<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) }) }) }} >

推荐答案

是的,它可能在FileReader和csv中使用:

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: developer.mozilla/en/docs /Web/API/FileReader csv软件包: www.npmjs/package/csv

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

更多推荐

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

本文发布于:2023-07-27 05:26:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1220601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   客户端   DropZone   React   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!