hussain hussain - 1 month ago 6
Javascript Question

How to check file content using nodejs?

I want to see content of the file that is posted from the client i am using

fs
module so with below code contents is coming undefined , Any idea what is missing in below code ?

I have
file
printed in server side to make sure i am gettign the data.

server.js

var data = new multiparty.Form();
var fs = require('fs');

export function create(req, res) {
data.parse(req, function(err,files) {
var file = files.file;
console.log(file);
fs.readFile(file, 'utf8', function(err, contents) {
console.log('content',contents);
});
});
};

Answer

I guess the problem might be the signature of the callback you are supplying to data.parse (you are missing the fields argument).
Check it yourself by looking to the examples on multiparty docs

var data = new multiparty.Form();
var fs = require('fs');

export function create(req, res) {
    data.parse(req, function(err, fields, files) {
        var file = files.file;
        console.log(file);
        fs.readFile(file, 'utf8', function(err, contents) {
            console.log('content',contents);
        });
    });
};