Skip to main content

How To Use jQuery To Post A CSV File To A Node.js Server

· 2 min read
Andrew Golightly

If you have a front-end that needs to accept a CSV file, upload it to a server using jQuery, and then have that Node.js server process that CSV file, then this a way to do it.

First up, the HTML

<form id="2020data">
<input type="file" name="csvFile">
<button type="submit" class="btn btn-default">Upload CSV File</button>

View this gist on GitHub

and then the jQuery that takes the selected CSV file, and uploads it to a server

// using jQuery
$("#2020data").submit(function(e) {

url: "",
type: "POST",
data: new FormData(this),
processData: false,
contentType: false

return false;

View this gist on GitHub

and finally the Node.js file that will accept and process it using Express (with the multer middleware), and then iterate through the CSV file.

As it turns out, the Node.js CSV libraries I found on npm were either not working or hadn’t been updated in a long time. To process a CSV file with standard JS using functions like split and replace made this relatively easy to do on my own.

const express = require('express');
const multer = require('multer');

const app = express();
const upload = multer();'/test', upload.single('csvFile'), async (req, res) => {
try {
const csvFile = req.file.buffer.toString();
const rows = csvFile.split('\n');

for (let row of rows) {
const columns = row.replace(/"/g, '').split(',');

} catch (err) {

app.listen(3000, function() {
console.log(`Server started at ${new Date().toString()}`);

View this gist on GitHub

Any questions let me know!