nolags
nolags

Reputation: 633

show filenames of file upload with console.log(req.files)

I have a file upload with node js and multer and want to show the names of the uploaded files in the terminal. But I only get Selected Files: [object Object],[object Object],[object Object]

router.post('/upload', function(req,res){
    upload(req,res,function(err) {
        console.log('Selected Files: ' + req.files);
        if(err){
            res.end("Error: '" + err + "'");
        }else{
        console.log('Files uploaded!');
        res.sendStatus(204);
        }
    });
});

I also tried req.files.filename but it gave me the output: Selected Files: undefined

How can I show the names of the uploaded files?

Upvotes: 0

Views: 1537

Answers (2)

Abhishek Rathore
Abhishek Rathore

Reputation: 1186

To get complete detail about the file use

console.log(req.file);

Upvotes: 0

Willi Pasternak
Willi Pasternak

Reputation: 486

you can change

console.log('Selected Files: ' + req.files);

to

console.log('Selected Files: ', req.files);

Modern Browsers are able to display the data right.

EDIT:

If you only want the file names it´s not possible without collecting all the file names with one function and then print that out for example:

var fileNames = [];
req.files.forEach(function(element){
   fileNames.push(element.filename);
})

console.log('Selected Files: ', fileNames);

Upvotes: 1

Related Questions