
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to read data from *.CSV file using JavaScript?
In this article, we are going to learn how to read data from *.CSV file using JavaScript.
To convert or parse CSV data into an array,we need JavaScript’s FileReader class, which contains a method called readAsText() that will read a CSV file content and parse the results as string text.
If we have the string, we can create a custom function to turn the string into an array.
To read a CSV file, first we need to accept the file.
Now let’s see how to accept the csv file from browser using HTML elements.
Example
Following is the example program to accept the csv file from the browser.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form id="myForm"> <input type="file" id="csvFile" accept=".csv" /> <br /> <input type="submit" value="Submit" /> </form> </body> </html>
We can now choose a csv file that need to be read.
Now let’s write a JavaScript code that read the csv file chosen.
Example
Following is the example program, which accept and reads the csv file.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form id="myForm"> <input type="file" id="csvFile" accept=".csv" /> <br /> <input type="submit" value="Submit" /> </form> <script> const myForm = document.getElementById("myForm"); const csvFile = document.getElementById("csvFile"); myForm.addEventListener("submit", function (e) { e.preventDefault(); const input = csvFile.files[0]; const reader = new FileReader(); reader.onload = function (e) { const text = e.target.result; document.write(text); }; reader.readAsText(input); }); </script> </body> </html>
- Related Articles
- How to read data from .csv file in Java?
- How to read the data from a CSV file in Java?\n
- How to read data from a file using FileInputStream?
- How to read CSV file in Python?
- Writing data from database to .csv file
- How to read data from JSON array using JavaScript?
- How to read data in from a file to String using java?
- Read Data from a Text File using C++
- How to import csv file data from Github in R?
- How to append data into a CSV file using PowerShell?
- Plot data from CSV file with Matplotlib
- Write data from/to a .csv file in java
- How to read a Pandas CSV file with no header?
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- How to read the data from a file in Java?
