27

there are all kinds of posts about this, but I'm still not getting it. I want to upload a *.csv and read and process its contents.

my jade file is this

//views/import.jade
extends layout
block content
h1= title
form(action="/import", method="post", enctype="multipart/form-data")
    input(type="file", name="ufile")
    input(type="submit", name="Upload")

--

I changed the code, but req.files is undefined

//routes/index.js

/* import page. */
router.get('/blah', function(req, res, next) {
  res.render('import', { title: 'Import Data' });
});

router.post('/import', function(req, res) {
    console.log(req.files);
});


module.exports = router;
Simply Seth
  • 3,246
  • 17
  • 51
  • 77

2 Answers2

16

Convert the uploaded file in to string, using

toString('utf8')

you can than make any operation on string like convert it to json using csvtojson package

Here is the sample code for uploading csv and than convert to json-

/* csv to json */

const express = require("express"),
  app = express(),
  upload = require("express-fileupload"),
  csvtojson = require("csvtojson");

let csvData = "test";
app.use(upload());

app.get("/", (req, res, next) => {
  res.sendFile(__dirname + "/index.html");
});

app.post("/file", (req, res) => {
/** convert req buffer into csv string , 
*   "csvfile" is the name of my file given at name attribute in input tag */
  csvData = req.files.csvfile.data.toString('utf8');
  return csvtojson().fromString(csvData).then(json => 
    {return res.status(201).json({csv:csvData, json:json})})
});

app.listen(process.env.PORT || 4000, function(){
  console.log('Your node js server is running');
});

working example- csvjsonapi

Vino
  • 892
  • 2
  • 16
  • 26
  • this is exactly what i am trying to do, but for some reason it isn't working for me. The toString() method on the buffer object just returns empty. I tried your working example and it worked fine with the same file so i really dont know why it would work here but not on my code, im accessing req.files.[name].data and it returns the buffer, adding the toString() method doesnt return the expected string. IS there a way to feed the raw data directly into csvtojson? – xunux Jul 26 '21 at 19:13
5

Hope this solves your question, this is my method to multiple upload file:

Nodejs :

router.post('/upload', function(req , res) {
    var multiparty = require('multiparty');
    var form = new multiparty.Form();
    var fs = require('fs');
    
    form.parse(req, function(err, fields, files) {  
        var imgArray = files.imatges;
    
    
        for (var i = 0; i < imgArray.length; i++) {
            var newPath = './public/uploads/'+fields.imgName+'/';
            var singleImg = imgArray[i];
            newPath+= singleImg.originalFilename;
            readAndWriteFile(singleImg, newPath);           
        }
        res.send("File uploaded to: " + newPath);
    
    });
    
    function readAndWriteFile(singleImg, newPath) {
    
            fs.readFile(singleImg.path , function(err,data) {
                fs.writeFile(newPath,data, function(err) {
                    if (err) console.log('ERRRRRR!! :'+err);
                    console.log('Fitxer: '+singleImg.originalFilename +' - '+ newPath);
                })
            })
    }
})

Make sure your form tag has enctype="multipart/form-data" attribute.

I hope this gives you a hand ;)

Max Base
  • 639
  • 1
  • 7
  • 15
Despertaweb
  • 1,672
  • 21
  • 29