Created
September 19, 2013 00:50
-
-
Save dylants/6617821 to your computer and use it in GitHub Desktop.
Upload CSV (Excel spreadsheet) data to a listening Node.js controller.
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
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); | |
}); | |
}); | |
}; |
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
<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> |
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
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...