Last active
August 2, 2017 21:36
-
-
Save magician11/94e7e900b9b390fb4a53b1d32cc38476 to your computer and use it in GitHub Desktop.
How To Use jQuery To Post A CSV File To A Node.js Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using jQuery | |
$("#2020data").submit(function(e) { | |
$.ajax({ | |
url: "https://e0d92634.ngrok.io/test", | |
type: "POST", | |
data: new FormData(this), | |
processData: false, | |
contentType: false | |
}); | |
return false; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="2020data"> | |
<input type="file" name="csvFile"> | |
<button type="submit" class="btn btn-default">Upload CSV File</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const multer = require('multer'); | |
const app = express(); | |
const upload = multer(); | |
app.post('/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(','); | |
console.log(columns); | |
} | |
res.sendStatus(200); | |
} catch (err) { | |
console.log(err); | |
res.sendStatus(400); | |
} | |
}); | |
app.listen(3000, function() { | |
console.log(`Server started at ${new Date().toString()}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A bit of a walkthrough here https://golightlyplus.com/use-jquery-post-csv-file-node-js-server/