Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CSV upload using nodeJS.

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 6.47k
    Comment on it

    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)


  • It is working Getting Below Error

      
    TypeError: Cannot read property 'file' of undefined
        at module.exports.uploadFile (/Users/anjan/Documents/vaptDashboard/controllers/uploadFileController.js:11:31)
        at Layer.handle [as handle_request] (/Users/anjan/Documents/vaptDashboard/node_modules/express/lib/router/layer.js:95:5)
        at next (/Users/anjan/Documents/vaptDashboard/node_modules/express/lib/router/route.js:137:13)
        at Route.dispatch (/Users/anjan/Documents/vaptDashboard/node_modules/express/lib/router/route.js:112:3)
        at Layer.handle [as handle_request] (/Users/anjan/Documents/vaptDashboard/node_modules/express/lib/router/layer.js:95:5)
        at /Users/anjan/Documents/vaptDa
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: