28

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;
5
  • Which middleware do you use with express to handle file upload ? What is at app.js line 30 ? Commented Aug 17, 2014 at 0:30
  • you're sending your form with method POST but declaring a GET route. The first step is to change your route to router.post('/import'... and try again. Commented Aug 17, 2014 at 0:30
  • changed the code ... no dice. Commented Aug 17, 2014 at 2:15
  • are you using the bodyparser middleware? Commented Aug 18, 2014 at 0:52
  • I am not using bodyparser middleware. Was not even aware of its necessity. Will go and research. Thanks. Commented Aug 18, 2014 at 2:46

2 Answers 2

17

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

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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 ;)

1 Comment

Thanx @TirthrajBarot ;) I'm just a beginner I try to do my best :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.