-3

I am new to Angular JS and Node Js. I want achieve one requirement. I have one page with upload button and I am uploading csv file and read the all records from CSV file and send to server side (node js).

In Node js will get the data from angular Js and save to MySQL database. How can I implement this requirement? Can someone help me this requirement. Thank You in Advance.

 Below is my **Angular Code:**<html><head><meta charset="UTF-8">    <title>File Upload Home</title></head><body ng-app = "myApp"><div ng-controller = "myCtrl"><table align="center" ><tr><td>Choose .csv file</td>
            <td><input type="file" name="readCsvFile" id="readCsvFile" accept=".csv"> </td><td><input type="button" value="Upload" ng-click="uploadCsvFile()"></td></tr> </table></div><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script>  var myApp = angular.module('myApp', []);   myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){    
$scope.uploadCsvFile = function() {     
    $http.post('/').success($scope.processData);
    /* $http({
      method: 'POST',
      url: '/',
      headers: {'Content-Type': undefined}
   }).success($scope.processData); */
};   $scope.processData = function(csv){  var lines=csv.split("\n");  var result = [];  var headerscsv=lines[0].split(";");  for(var i=1;i<lines.length;i++){
  var obj = {};
  var currentline=lines[i].split(",");
  for(var j=0;j<headerscsv.length;j++){
      obj[headerscsv[j]] = currentline[j];
  }
  result.push(obj);  }
return JSON.stringify(result); //JSON  }    }]); </script> </body> </html>

**Node Js Code:**var express = require('express');var bodyParser =require('body-parser');var app = express();var mysql = require('mysql');//var expressValidator = require('express-validator');app.use(bodyParser.urlencoded({extended: true }));app.use(bodyParser.json({ type: '*/*' }));var connection =mysql.createConnection({ host: 'localhost', user: '', password: '', database: 'dbname'});connection.connect(function(err){ if(err){ console.log('Database connection error'); }else{ console.log('Database connection successful'); }}); app.post('/', function(req, res) {var jsondata = JSON.parse(JSON.stringify(req.body));var values = [];for(var i=0; i<jsondata.length; i++){ values.push([jsondata[i].eno,jsondata[i].name,jsondata[i].age]);}connection.query('INSERT INTO members (eno, name, age) VALUES ?', [values], function(err,result) { if(err) { res.send('Error'); } else { res.send('Success'); }});});app.use(express.static('static'));var server =app.listen(3000,function(){ console.log('running on 3000...'); });

suresh
  • 11
  • 1
  • 2
  • You are asking to design a prototype of an app (The inputs aren't sufficient). Plz do post your code. Then probably you might get help. – Ram Mar 21 '17 at 14:02
  • Hi Can you check my code and suggest me how to implement that requirement. Thanks – suresh Mar 22 '17 at 04:17
  • I am not aware of Node JS, but in AngularJS use the link http://stackoverflow.com/questions/26353676/how-to-read-csv-file-content-in-angular-js to read csv file and then convert the data into JSON using https://github.com/bahaaldine/angular-csv-import then at last write Angular $http service to post this JSON data to server. – Ram Mar 22 '17 at 19:23

1 Answers1

1

If you are using MYSql with Node.js and Angular JS

I think the simplest implementation you can do is :

  1. Create simplest form in HTML/AngularJS and use this module to enable file upload from browser https://github.com/danialfarid/ng-file-upload
  2. Create a route in Node and use multer to get the uploaded file and save it on your server. https://www.npmjs.com/package/multer
  3. Use node module csvtojson to convert the csv data/file to json data to use easily https://www.npmjs.com/package/csvtojson
  4. Use Sequelize ORM to make connection to MySQL and inserting data. It enables you to use json format to deal with sql databases So you dont need to write RAW queries. https://www.npmjs.com/package/sequelize
deepakchauhan
  • 290
  • 1
  • 2
  • 8
  • Hi Thanks for giving reply and any sample application is available for this requirement and if you have any sample links or applications that will helps to me for developing that requirement and i am new to both technologies. Could you please help me. – suresh Mar 21 '17 at 09:51