Search code examples
node.jsfile-uploadfilenames

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?


Solution

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