Node.js is the server side scripting language. In this blog, I am explaining how I will upload the CSV file using Node.Js.
Here is the step by step implementation of CSV upload.
Step 1:
In the first step, we will create a view page where we upload a CSV file. In routes we will define the URL in which our view page will open:
app.get('/upload', function(request, response) {
response.render('uploadFile.html');
});
Step 2:
Now we will create a view page.Here I have created a file named as uploadFile.html.
<form action="/uploadCsv" method="post" enctype="multipart/form-data" class="box login">
<input type="file" name="file">
<input type="submit" class="" value="Upload" tabindex="1">
</form>
Step 3:
Now we will again go to the routes and give the URL for getting the request which we will give after clicking on the upload button.
app.post('/uploadCsv', uploadFileController.uploadCsv);
Step 4:
Now we will create a uploadFileController and make a function uploadCsv and write the below code to upload file.
var fs = require("fs");
module.exports.uploadCsv = function(req, res) {
var today = new Date();
var date = today.getDate();
var tempPath = req.files.file.path,
targetPath = path.resolve('./uploads/'+req.files.file.originalFilename);
if (path.extname(req.files.file.name).toLowerCase() === '.csv') {
fs.rename(tempPath, './uploads/csv_1', function(err) {
if (err) throw err;
console.log("Upload completed!");
});
};
Here our file upload steps are completed.By this way, we can implement CSV file uploading using nodeJS.
Note:For reading the files we have to upload fs module by using npm only then we will use fs module.
1 Comment(s)