how to upload and read a file with nodejs / express
Asked Answered
D

2

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;
Downcome answered 17/8, 2014 at 0:25 Comment(5)
Which middleware do you use with express to handle file upload ? What is at app.js line 30 ?Old
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.Naturally
changed the code ... no dice.Downcome
are you using the bodyparser middleware?Melisent
I am not using bodyparser middleware. Was not even aware of its necessity. Will go and research. Thanks.Downcome
W
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

Warr answered 12/9, 2018 at 9:54 Comment(1)
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?Amine
P
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 ;)

Pelasgian answered 11/10, 2016 at 19:39 Comment(1)
Thanx @TirthrajBarot ;) I'm just a beginner I try to do my best :PPelasgian

© 2022 - 2024 — McMap. All rights reserved.