Accesing filenames in req.files created by Multer, doing stuff to newly uploaded files

330
January 19, 2017, at 03:55 AM

I'm new to web development. I have a multi-file upload on a page. The files are successfully uploaded with multer into my 'public/uploads' folder.

How do I access the filenames that I just uploaded using the req.files that multer creates? The result is not an array, although the multer docs say for multiple files, req.files will contain an array.

I feel like I have something set up incorrectly.

Here's console.log(req.files). as you can see the backets are not comma separated.

[ { fieldname: 'file',
    originalname: 'acschemrev5b00690.jpeg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'public/uploads',
    filename: '1484769679031-acschemrev5b00690.jpeg',
    path: 'public/uploads/1484769679031-acschemrev5b00690.jpeg',
    size: 79299 } ]
[ { fieldname: 'file',
    originalname: 'acschemrev5b00701.jpeg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'public/uploads',
    filename: '1484769679047-acschemrev5b00701.jpeg',
    path: 'public/uploads/1484769679047-acschemrev5b00701.jpeg',
    size: 127608 } ]
[ { fieldname: 'file',
    originalname: 'acschemrev5b00703.jpeg',
    encoding: '7bit',
    mimetype: 'image/jpeg',
    destination: 'public/uploads',
    filename: '1484769679109-acschemrev5b00703.jpeg',
    path: 'public/uploads/1484769679109-acschemrev5b00703.jpeg',
    size: 117312 } ]

Here's my server app.js file:

var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multer = require('multer');
// var upload = multer({ dest: 'uploads/' });

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/uploads')
  },
  filename: function (req, file, cb) {
    // cb(null, file.fieldname + '-' + Date.now());
    cb(null, Date.now() + "-" + file.originalname);
    // cb(null, file.originalname);
  }
});
var upload = multer({ storage: storage });


// set the view engine to pug
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
app.get('/', function (req, res) {
  res.render('index');
});
// from multer
app.post( '/file-upload', upload.any(), function( req, res, next ) {
  // Metadata about the uploaded file can now be found in req.file
  res.send(req.files);
  // console.log(req.files);
  currentFiles = req.files;
  console.log(req.files);
app.post('/crop-images')
});

I need to process the files that I just uploaded. What is the best way to do this? Do I need to create an array of each filename and then process each one? How do I get their names out of req.files?

Thanks!

Answer 1

If you want to retrieve filenames from the req.files, you could just map that over it like that:

var filenames = req.files.map(function(file) {
  return file.filename; // or file.originalname
});
READ ALSO
Writing binary data to Buffer

Writing binary data to Buffer

Normally, I would expect that the following would be good enough to represent binary data in a Buffer:

351
Error:Can't set headers after they are sent in express

Error:Can't set headers after they are sent in express

I am exploring node and express with redux where I want to set a cookie after the page has been rendered and wanna use the updated state for setting a cookie where I am getting this error

503
Nodejs - Mocha, Chai: How to test an end point that passes control to multiple middleware functions by calling next

Nodejs - Mocha, Chai: How to test an end point that passes control to multiple middleware functions by calling next

I have an end point that passes control to multiple middleware functions by calling next() for example:

297
set custom headers dynamically in express app to be accessible by angular app

set custom headers dynamically in express app to be accessible by angular app

i am quiet new to expressI have searched a lot and unable to find the answer

355