Reputation: 1615
I want to covert CSV file to JSON Array object using file upload.
i have tried multer,
module.exports = function(express, app, upload){
var router = express.Router();
var util = require('util');
router.get('/',function(req,res){
res.send(200);
});
router.post('/csv', upload,function(req,res){
//How to get file contents ?
res.send('/dashboard');
});
app.use('/upload',router);
};
where upload is,
var upload = multer({ storage: storage }).single('csvfile');
Upvotes: 2
Views: 5308
Reputation: 7077
Jay.
If you have small csv files you can use memory for multer.
var upload = multer({ inMemory: true}).single('csvfile');
And after this you must transform csv
router.post('/csv', upload, function(req,res){
var csvString = req.files.csvfile.buffer.toString()
converter.fromString(csvString, function(err,result){
if(err)return res.send("ERR")
res.send(result);
});
});
Upvotes: 4
Reputation: 15638
You simply need to extract a body from the request. bodyparser
https://github.com/expressjs/body-parser is a fine tool for doing this.
First, install and require the library. Then, create a text
bodyparser (probably the best fit for csv
use-case) and register it as a middleware:
app.use(bodyParser.json()) // app being 'var app = express()' server
finally:
router.post('/csv', upload,function(req,res){
console.log(req.body) // req.body should be populated by request body
res.send('/dashboard');
});
Note however, that bodyparser
is not suited for multipart bodies. If this is your case, use something as https://github.com/felixge/node-formidable
Upvotes: 1