hussain
hussain

Reputation: 7123

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

Upvotes: 0

Views: 1490

Answers (1)

Francesco Pezzella
Francesco Pezzella

Reputation: 1795

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

Upvotes: 1

Related Questions