Skip to content

Instantly share code, notes, and snippets.

@dylants
Created September 19, 2013 00:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dylants/6617821 to your computer and use it in GitHub Desktop.
Save dylants/6617821 to your computer and use it in GitHub Desktop.
Upload CSV (Excel spreadsheet) data to a listening Node.js controller.
var request = require("request"),
csv = require("csv");
module.exports = function(app) {
// accepts the POST form submit of the CSV file
app.post("/upload/data", function(req, res) {
// the name under "files" must correspond to the name of the
// file input field in the submitted form (here: "csvdata")
csv().from.path(req.files.csvdata.path, {
delimiter: ",",
escape: '"'
})
// when a record is found in the CSV file (a row)
.on("record", function(row, index) {
var firstName, lastName;
// skip the header row
if (index === 0) {
return;
}
// read in the data from the row
firstName = row[0].trim();
lastName = row[1].trim();
// perform some operation with the data
// ...
})
// when the end of the CSV document is reached
.on("end", function() {
// redirect back to the root
res.redirect("/");
})
// if any errors occur
.on("error", function(error) {
console.log(error.message);
});
});
};
<section id="upload-data-panel">
<form id="upload-form" action="/upload/data" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Upload Data</legend>
<div>
<!-- The name here is important, and will be used later to reference the data -->
<input type="file" name="csvdata" accept="text/cvs">
</div>
<div>
<input type="submit" value="Submit"/>
</div>
</fieldset>
</form>
</section>
@suriyaJaay
Copy link

am unable to understand your above code about how to run this ? could you pls open repo/tutorial for how upload csv using MEAN stack and how to load csv data from db to html/view page(table)?

Thank you...

@guru9
Copy link

guru9 commented Jan 8, 2018

don't mention 'action="/upload/data"' in form. It will redirected to another page.
So please use controller or services to upload a file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment