Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 15808  / 3 Years ago, sat, june 12, 2021, 12:00:00

I want to get csv file from input tag and convert data of csv file into json object. Is there any plugin in react js or any custom code ?


More From » reactjs

 Answers
8

You can use an external library like Papa Parse to parse the CSV data.


A simple input tag with type as file would work to read the CSV data.


      <input
type="file"
accept=".csv,.xlsx,.xls"
onChange={handleFileUpload}
/>

Please declare handleFileUpload function and use the library inside to parse the read data.


Here's an example which will read a CSV file and log the corresponding JSON:


import Papa from "papaparse";

export default function App() {
return (
<div className="App">
<input
type="file"
accept=".csv,.xlsx,.xls"
onChange={(e) => {
const files = e.target.files;
console.log(files);
if (files) {
console.log(files[0]);
Papa.parse(files[0], {
complete: function(results) {
console.log("Finished:", results.data);
}}
)
}
}}
/>
</div>
);
}

[#50263] Thursday, May 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
osvaldo

Total Points: 102
Total Questions: 95
Total Answers: 102

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;